问题
i'm learning MongoDB for a few weeks now and i have still no idea how to query nested documents in my project. I read the MongoDB-docs and googled a lot, but i found no good explanations or tutorials for my problem. Maybe you can help me!
I have a Collection with the following structure (here the JSON output):
{
community: "1",
date: 1365680790125,
isIndoor: "true",
party: {
teams: [
{
isWinner: true,
players: [
{
name: "jim",
hits: 4,
isFinisher: true
},
{
name: "john",
hits: 6,
isFinisher: false
}
]
},
{
isWinner: false,
players: [
{
name: "mike",
hits: 6,
isFinisher: false
},
{
name: "moe",
hits: 3,
isFinisher: false
}
]
}
]
},
id: "3141be7f9988d872"
}
And now I want to get all records where 'jim' participated.
I tried this query (in coffeescript), but i get no results and i think its very crappy...
query = party:
$all:[
teams:
$in: [
player: [
name: 'jim'
]
]
]
I hope you can help me understand querying nested documents. Thanks!
回答1:
Use dot notation to query against the fields of embedded objects:
query = { 'party.teams.players.name': 'jim' }
来源:https://stackoverflow.com/questions/15948762/querying-nested-in-mongodb