1

I using mongodb to save my user data along with array. My question is how to retrieve multiple objects that matches the given value in that user array.like this:

{ 
    _id: { objid:'0000000000000' },
    userId: 'abc',
    userItems : [
        {
            itemName: 'mango',
            date: 24,
            month: 1,
            year: 2016
        },
        {
            itemName: 'apple',
            date: 24,
            month: 1,
            year: 2016
        },
        {
            itemName: 'orange',
            date: 22,
            month: 1,
            year: 2016
        },
        {
            itemName: 'vanilla',
            date: 23,
            month: 1,
            year: 2016
        }
    ]
}

and expeccted result is

{
    _id: { objid: '0000000000000' },
    userId: 'abc',
    userItems: [
        {
            itemName: 'mango',
            date: 24,
            month: 1,
            year: 2016
        },
        {
            itemName: 'apple',
            date: 24,
            month: 1,
            year: 2016
        }
    ]
}

I want all the element that matches the date,month,year from this userId userItems array please help me out from this

styvane
  • 59,869
  • 19
  • 150
  • 156

2 Answers2

1

We can find result by aggregation framework.

db.user.aggregate(
   {
     $unwind : "$userItems" 
   },
   {
      $match: {
        "userItems.date" : 24, 
        "userItems.month" : 1, 
        "userItems.year" : 2016
      }
   },
   {
      $group: {
         "_id" : { "id":"$_id","userId":"$userId"},
         "userItems" : {$push:"$userItems"}
      }
   },
   {
       $project:{
          "_id": "$_id.id", 
          "userId": "$_id.userId",
          "userItems": 1 
       }
   }
)
Somnath Muluk
  • 55,015
  • 38
  • 216
  • 226
  • I have seen that format before, but I have never used it, so I can't comment on whether or not the best way. Upvoting anyway, because I know it's one of the ways to do it. – VSO Jan 29 '16 at 19:26
  • 1
    This is the best thing in mongo, you must learn. Aggregation framework is good for Grouping and projecting required data. Best when collection size is getting bigger and bigger. – Somnath Muluk Jan 29 '16 at 19:28
  • Thanks, I will check it out. – VSO Jan 29 '16 at 20:09
  • Thank you all this answer solved my problem but for my issue i dont need $projection. Thanks Thankyou all – Muhammad Sami Jan 31 '16 at 09:56
  • Welcome. I have given $projection to give knowledge about how you can project result coming from group. This project will give object similar to your input. You can use projection or not, that's your need. If someone pointing to you to right direction or answer, you should accept answer. You must not have visited the link I have given in last comment. People see your acceptance ratio. – Somnath Muluk Feb 01 '16 at 05:32
0
db.collectioName.find(
   {
      nameOfArrayYouarelookingIn: {
         $elemMatch: {
            ArrayFieldOneName: 1,
            ArrayFieldTwoName: 'blahblah'
         }
      }
   }
)

See $elemMatch documentation for more details and examples.

VSO
  • 11,546
  • 25
  • 99
  • 187