1

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 Answer 1

1

Use dot notation to query against the fields of embedded objects:

query = { 'party.teams.players.name': 'jim' }
Sign up to request clarification or add additional context in comments.

3 Comments

hey, thanks for answer! I tried your query on my database (deployd platform) and it is not working.. i get all records and not only the records with player-name 'jim'. Then I tried your query in the mongoDB Browser Shell and it worked...
okay... it is a bug in deployd. it works now, see: groups.google.com/forum/#!msg/deployd-users/NidQ1sT9u6o/…
I'm having the exact same problem, @rojan21, deployd return all records on any query, but I can't find information about this bug...

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.