How to write find query to get duplicate data from Arrays

2,404 views
Skip to first unread message

Nagamuni Reddy

unread,
Apr 23, 2013, 6:09:26 PM4/23/13
to mongod...@googlegroups.com
inpur Array:
 
{
  "_id" : ObjectId("5176b844ad366e8c93f96d85"),
  "userInfo" : [{
        "userId" : "100",,
        "empid" : "2002"  
     },
   {
      "userId" : "200",
      "empid" : "404"
    }, {
      "userId" : "200",
      "empid" : "4000"
    }, {
      "userId" : "200",
      "empid" : "4000"
    }, {
      "userId" : "200",
      "empid" : "4000"
    }, {
      "userId" : "2002",
      "empid" : "4000"
    }, {
      "userId" : "2003",
      "empid" : "4000"
    }]
}
 
i have written query :     db.collection.find({ "userInfo.userId" : "200" }, { "userInfo.$" : 1 });

in the input array four records are having userId" : "200".but it is return only one record  like

 

{
  "_id" : ObjectId("5176b844ad366e8c93f96d85"),
  "userInfo" : [{
      "userId" : "200",
      "empid" : "404"
         "empid" : "4000"
    }]
}
 
But i want to display all records(whose userId" : "200")  from  array like below.
 
{
  "_id" : ObjectId("5176b844ad366e8c93f96d85"),
  "userInfo" : [{
        "userId" : "200",
      "empid" : "404"
    }, {
      "userId" : "200",
      "empid" : "4000"
    }, {
      "userId" : "200",
      "empid" : "4000"
    }, {
      "userId" : "200",
      "empid" : "4000"
    }]
}

 

 

please send the query for this one.

 

Thanks and Regards,

N.NagaMuniReddy.

 

 

Asya Kamsky

unread,
Apr 24, 2013, 12:11:47 AM4/24/13
to mongod...@googlegroups.com
There is no way to do this with a simple find.  You can write an aggregation framework pipeline to get back what you want or you can consider structuring your schema differently if you require getting this data via a find statement.

Nagamuni Reddy

unread,
Apr 24, 2013, 10:20:04 AM4/24/13
to mongod...@googlegroups.com

Please send query how to write an aggregation framework pipeline for that.

 
--
--
You received this message because you are subscribed to the Google
Groups "mongodb-user" group.
To post to this group, send email to mongod...@googlegroups.com
To unsubscribe from this group, send email to
mongodb-user...@googlegroups.com
See also the IRC channel -- freenode.net#mongodb
 
---
You received this message because you are subscribed to the Google Groups "mongodb-user" group.
To unsubscribe from this group and stop receiving emails from it, send an email to mongodb-user...@googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.
 
 

masiegelmtr

unread,
Apr 24, 2013, 1:03:11 PM4/24/13
to mongod...@googlegroups.com
Hint: You'll probably want an $unwind operation to separate the userInfo array into one document per entry in the array.  Then, you can do a $match on the userId.

Gary R

unread,
Apr 24, 2013, 4:34:19 PM4/24/13
to mongod...@googlegroups.com
Perhaps an aggregation pipeline something like this:
db.collection.aggregate({$unwind:'$userInfo'}, {$match:{ 'userInfo.userId':'200'}}, {$group:{_id:'$_id',userInfo:{$push:'$userInfo'}}})

Certainly, only you know if it's worth going through the entire collection, or if a large number of docs could be eliminated with a $match at the beginning.

Nagamuni Reddy

unread,
Apr 24, 2013, 5:21:49 PM4/24/13
to mongod...@googlegroups.com
Thanks a lot Gary.It is working fine.

Reply all
Reply to author
Forward
0 new messages