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

3

2 Answers 2

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 
       }
   }
)
Sign up to request clarification or add additional context in comments.

4 Comments

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.
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.
Thank you all this answer solved my problem but for my issue i dont need $projection. Thanks Thankyou all
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.
0
db.collectioName.find(
   {
      nameOfArrayYouarelookingIn: {
         $elemMatch: {
            ArrayFieldOneName: 1,
            ArrayFieldTwoName: 'blahblah'
         }
      }
   }
)

See $elemMatch documentation for more details and examples.

2 Comments

I have used it but it only gives me the first matching object not more than that
this will match whole document. and not give matching objects inside of array property. @VSO check my answer

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.