Alejo Ribes
Alejo Ribes

Reputation: 1415

Return of Mongod Query Distinct

I have the next problem: When I do a query with a distinct, only returns a array of name properties, example:

var promise = clients.distinct("name", {"id_client_group": id_clientGroup, "id_client": id_client, "status": "active"}).exec();

return this:

["client2, client3"]

When I would need all the properties of the object Example:

[
{
    name: "client1",
    mail: "mail@1"
},
{
     name: "client2",
     mail: "mail@2"
}
]

I have tried to do a normal search and returns the array of objects complete

Upvotes: 1

Views: 77

Answers (1)

njLT
njLT

Reputation: 474

Based on the mongodb driver API doc, this is the expected behaviour for distinct.

The distinct command returns returns a list of distinct values for the given key across a collection.

You should try aggregation instead, but I'm not sure what kind of data you are looking for in case of repeated data.

clients.aggregate([
    {$match: {"id_client_group": id_clientGroup, "id_client": id_client, "status": "active"}},
    {$group: {_id: "$name", mail:{$first:"$mail"}}}
])

You might want to try some other expression in the group stage, checkout the doc here

Upvotes: 4

Related Questions