0

i have the following document in a collection:

{
    "_id" : 101,
    "students" : [ 
        {
            "name" : "john",            
            "age" : 10,
            "city":'CA'
        },
        {
            "name" : "danial",            
            "age" : 15,
            "city":'KA'
        }
    ]
}

{
    "_id" : 102,
    "students" : [ 
        {
            "name" : "adam",            
            "age" : 20,
            "city":'NY'
        },
        {
            "name" : "johnson",            
            "age" : 12,
            "city":'CA'
        }
    ]
}

And i fire the following query:

db.data.find({'students.city':'CA'})

This returns me "students" objects from both the documents, as one instance matches the filter ("city"; 'CA') in both.

However, i desire to only get the matching array in the result. That is, i want the following result:

{
    "_id" : 101,
    "students" : [ 
        {
            "name" : "john",            
            "age" : 10,
            "city":'CA'
        }
    ]
}

{
    "_id" : 102,
    "students" : [        
        {
            "name" : "johnson",            
            "age" : 12,
            "city":'CA'
        }
    ]
}

Please help.

Anup
  • 9,396
  • 16
  • 74
  • 138

2 Answers2

3

You need to use $elemMatch in your projection:

> db.data.find({'students.city':'CA'},{ students:{ $elemMatch:{'city':'CA'} }})
{ "_id" : 101, "students" : [ { "name" : "john", "age" : 10, "city" : "CA" } ] }
{ "_id" : 102, "students" : [ { "name" : "johnson", "age" : 12, "city" : "CA" } ] }

Btw: I'd strongly suggest reading the Open letter to students with homework problems.

Community
  • 1
  • 1
Markus W Mahlberg
  • 19,711
  • 6
  • 65
  • 89
0

I think you should use an aggregation operation.

Here you have the documentation: https://docs.mongodb.org/manual/aggregation/

And a similar question: Retrieve only the queried element in an object array in MongoDB collection

Community
  • 1
  • 1
Telmo Ivo
  • 4,248
  • 1
  • 15
  • 23
  • Nope, that's plainly wrong. See [my answer](http://stackoverflow.com/a/35408883/1296707). – Markus W Mahlberg Feb 15 '16 at 12:02
  • 1
    yep. you are right. after confirming again. it's better your way. Correct me if I'm wrong: I said to use aggregate because I thought you cannot return multiple elements of an array matching your criteria using a find() operation. but since it's different documents and only 1 matching element per document.your way it's the right one. – Telmo Ivo Feb 15 '16 at 12:14
  • I haven't checked, but I think it could be possible to use an "$or" in the projection... – Markus W Mahlberg Feb 15 '16 at 13:54
  • @MarkusWMahlberg i tried to use $or inside the $elemMatch to return multiple elements, but it was not working. – Gopal Jun 16 '20 at 09:01