I'm trying to retrieve a list of sub arrays of a document which meets a particular condition.
"_id" : "something",
"players" : [
{
"Name" : "Sunny"
"score": 20
},
{
"Name" : "John"
"score" : 40
},
{
"Name" : "Alice"
"score" : 20
},
etc...
]
I wanted output of those with score = 20 like
{
"Name" : "Sunny"
"score": 20
},
{
"Name" : "Alice"
"score" : 20
}
But I tried querying with:
db.collection.find(
{ "players.score":20, "_id":"something" },
{ "players" :1 }
)
But it gives me all the 3 sub arrays like
{
"Name" : "Sunny"
"score": 20
},
{
"Name" : "John"
"score" : 40
},
{
"Name" : "Alice"
"score" : 20
}
If I use projector "$" or $matchelement like:
db.collection.find({ "players.$.score":20, "_id":"something" }
It gives the very first array example
{
"Name" : "Sunny"
"score": 20
}
Can anyone help me out with correct query for this . thanks in advance :)