Data & Question
Given a collection of horse race meetings (db.sectionals), how do I write the query so that it returns the arrays (i.e. the horses) whose position: [2] ?
[
{
"race": {
"date": ["2013-05-08"],
"meeting": ["canterbury park"],
"weather": ["fine"],
"field": {
"Belladini": {
"position": [2],
"distance": [1100]
},
"Bobs Destiny": {
"position": [7],
"distance": [1100]
}
}
}
},
{
"race": {
"date": ["2013-03-27"],
"meeting": ["canterbury park"],
"weather": ["fine"],
"field": {
"Berna": {
"position": [5],
"distance": [1550]
},
"Excited Prince": {
"position": [2],
"distance": [1550]
}
}
}
}
]
Desired result
In this exmaple I would like it to return something like
"Belladini": {
"position": [2],
"distance": [1100]
},
"Excited Prince": {
"position": [2],
"distance": [1550]
}
Attempts
I've found the the documentation on nested arrays and finding elements of arrays a bit confusing, and have tried various attempts, such as
db.Sectionals.find({ "race.field.position": 2} )
db.Sectionals.find({ "race.field": { $elemMatch: { position: 2} } } )
db.Sectionals.find({ "race.field": {$all: [{"position": 2}] }} )
without success.
Additional information
I'm using Robomongo.
race.fieldwhich is a problem. 2)race.fieldshould be array field