0

The below is the collection named testing which i am working on and I need to get all the persons who is having the DOB of "24-04-1973"

 {
        "year": "1973",
        "allPersons": [
            {
                "name": "First guy",
                "DOB": "24-04-1973"
            },
            {
                "name" : "Second guy",
                "DOB":  "02-12-1973"
            }     
            {
                "name": "Third guy",
                "DOB": "13-01-1973"
            },
            {
                "name": "Fourth guy",
                "DOB": "24-04-1973"
            },
            {
                "name": "Fifth guy",
                "DOB": "24-04-1973"
            },
        ]
    }

But I am only able to get the first object from the allPersons Array through the below query

 db.testing.find(
       {
              "allPersons.DOB":"24-04-1973"
       },
       {
              _id:0,"allPersons":
              {
                     $elemMatch:
                     {
                            "DOB":'24-04-1973'
                     }
              }
       }
).pretty();

Result of above Query

{
        "allPersons" : [
                {
                        "name" : "First guy",
                        "DOB" : "24-04-1973"
                }
        ]
}

But the result i am expecting to get is to have all the persons who have the DOB of "24-04-1973" like below

  {
            "allPersons" : [
                    {
                            "name" : "First guy",
                            "DOB" : "24-04-1973"
                    },
                    {        
                            "name" : "Fourth guy",
                            "DOB" : "24-04-1973"
                    },
                    {
                            "name" : "Fifth guy",
                            "DOB" : "24-04-1973"
                    }
            ]
    }

Does anybody know the correct query to generate the above kind of result

mongo Db version 2.6.11

I am new to this, Excuse if it's silly

3
  • Please mention your MongoDB version? Is it 3.2? Commented Mar 18, 2016 at 15:26
  • mongo db version 2.6.11 Commented Mar 18, 2016 at 15:35
  • See my post below. Commented Mar 18, 2016 at 15:50

1 Answer 1

2

Try following script:

db.doc.aggregate([
    {$unwind:"$allPersons"},
    {$match:{"allPersons.DOB":"24-04-1973"}},
    {$group:{_id:{id:"$_id", year:"$year"}, allPersons:{$push:"$allPersons"}}},
    {$project:{_id:0, allPersons:1}}  
])

It will emit:

{ 
    "allPersons" : [
        {
            "name" : "First guy", 
            "DOB" : "24-04-1973"
        }, 
        {
            "name" : "Fourth guy", 
            "DOB" : "24-04-1973"
        }, 
        {
            "name" : "Fifth guy", 
            "DOB" : "24-04-1973"
        }
    ]
}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.