Querying nested in mongoDB

和自甴很熟 提交于 2019-12-24 13:35:28

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!