0

I'm trying to query my MongoDB structure, specifically the users table to get a specific field, address, but am having difficulty because I'm not sure how to access something a couple levels in users > emails > address:

{
    "_id": "BkWk7hq4MRyMAyK4mm",
    "createdAt": ISODate("2015-11-15T19:46:41.633Z"),
    "services": {
        "password": {
            "bcrypt": "$2a$10$voVzU3pIVZBd1bfJf1oX4.OMPnzi8zXawYY5REtovPayBJL7dZLWSC"
        },
        "resume": {
            "loginTokens": []
        }
    },
    "emails": [{
        "address": "[email protected]",
        "verified": false,
        "provides": "default"
    }],
    "roles": {
        "J8Bhq3uTtdgwZdx3rz": ["guest", "account/profile"]
    }
} {
    "_id": "3qfCgFz9r5wKjnmymQ",
    "createdAt": ISODate("2015-12-15T19:49:05.236Z"),
    "emails": [],
    "roles": {
        "J8aBhq3uTtdgwZx3rz": ["anonymous", "guest"]
    },
    "services": {
        "resume": {
            "loginTokens": [{
                "when": ISODate("2015-12-15T19:49:05.280Z"),
                "hashedToken": "c1ybS3U3+GeC8ZNzGQ3WOctWpudQvv4vND6EzlRygtCQ="
            }]
        }
    }
}

I was trying to use the following:

db.users.find( { emails: { address : "[email protected]" } } )

1 Answer 1

1

You can query nested objects with dot notation.

db.users.find( { 'emails.address' : "[email protected]" } )

Your query is correct but it is querying documents for a complete match. So if your emails field had only address field it would work.

With dot notation you can check just one embedded field for matching.

Take a look at the documentation.

Sign up to request clarification or add additional context in comments.

1 Comment

Would be nice if you could add an explanation of why this is different to what the OP is trying and why what they are trying does not match so they understand it.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.