Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

mongoDB distinct & where in same query?

Tags:

mongodb

Let's say I have the following documents

Article { Comment: embedMany }  Comment { Reply: embedMany }  Reply { email: string, ip: string } 

I want to make a query that selects distinct Reply.ip where Reply.email = xxx

Something like this, only it doesn't work..

db.Article.find("Comment.Reply.email" : "xxx").distinct("Comment.Reply.ip") 

JSON export:

{    "_id":{       "$oid":"4e71be36c6eed629c61cea2c"    },    "name":"test",    "Comment":[       {          "name":"comment test",          "Reply":[             {                "ip":"192.168.2.1",                "email":"yyy"             },             {                "ip":"127.0.0.1",                "email":"zzz"             }          ]       },       {          "name":"comment 2 test",          "Reply":[             {                "ip":"128.168.1.1",                "email":"xxx"             },             {                "ip":"192.168.1.1",                "email":"xxx"             }          ]       }    ] } 

I run : db.Article.distinct("Comment.Reply.ip",{"Comment.Reply.email" : "xxx"})

I expect : ["128.168.1.1", "192.168.1.1"]

I get : ["127.0.0.1", "128.168.1.1", "192.168.1.1", "192.168.2.1"]

like image 765
Inoryy Avatar asked Sep 14 '11 16:09

Inoryy


People also ask

What does distinct do in MongoDB?

In MongoDB, the distinct() method finds the distinct values for a given field across a single collection and returns the results in an array. It takes three parameters first one is the field for which to return distinct values and the others are optional.

Can we use distinct in MongoDB?

To get unique values and ignore duplicates, use distinct() in MongoDB. The distinct() finds the distinct values for a specified field across a single collection and returns the results in an array.

How do I get distinct records in MongoDB?

To get distinct values, use distinct() in MongoDB. It finds the distinct values for a specified field across a single collection or view and returns the results in an array.

Where is distinct count in MongoDB?

Note that there are four records with a last_name value of “smith”. The four records have three distinct values for the first_name field (“mike, “william”, and “mark”). To count the unique values, use "distinct()" rather than "find()", and "length" rather than "count()".


1 Answers

Distinct query in mongo with condition works like this

 db.Article.distinct("Comment.Reply.ip",{"Comment.Reply.email" : "xxx"}) 

not other way around

EDIT:

I understand the problem now, inorder to match/filter subdocuments we need to use $elemMatch operator, like this

  db.Article.distinct("Comment.Reply.ip",{Comment: {$elemMatch: {"Reply.email" : "xxx"}}}) 

but this will not work if the sub-document contains sub arrays (in your case, you have array of replies). There is an existing issue $elemMatch on subArray is opened. And its planned for mongo 2.1. You can check out the link for more info

like image 148
RameshVel Avatar answered Oct 13 '22 20:10

RameshVel