MongoDB noob here...
I have a collection as follows...
> db.students.find({_id:22},{scores:[{type:'exam'}]}).pretty()
{
"_id" : 22,
"scores" : [
{
"type" : "exam",
"score" : 75.04996547553947
},
{
"type" : "quiz",
"score" : 10.23046475899236
},
{
"type" : "homework",
"score" : 96.72520512117761
},
{
"type" : "homework",
"score" : 6.488940333376703
}
]
}
how do I display only the quiz score via mongo shell?
You have some syntax in your original example which probably isn't doing what you expect .. that is, it looks like your intent was to only match scores for a specific type ('exam' in your example, 'quiz' by your description).
Below are some examples using the MongoDB 2.2 shell.
$elemMatch
projectionYou can use the $elemMatch projection to return the first matching element in an array:
The result will be the matching element of the array for each document, eg:
Aggregation Framework
If you want to display more than one matching value or reshape the result document instead of returning the full matching array element, you can use the Aggregation Framework:
The result in this case includes would be: