0
[
{
    customer: {
        field 1: {},
        field 2: {},
        answers: {
            "BUSI_EXP": { "answered": true, "updated": "date" },
            "NEW_HOME": { "answered": true, "updated": "date" },
            "VEHICLE": { "answered": true, "updated": "date" }
        }
    }
},
{
    customer: {
        field 1: {},
        field 2: {},
        answers: {
            "BUSI_EXP": { "answered": false, "updated": "date" },
            "NEW_HOME": { "answered": false, "updated": "date" },
            "VEHICLE": { "answered": true, "updated": "date" }
        }
    }
}
]

Here's my customer collection. As you can see, it has an answers field that has an answered flag for each key. How do i get all customers who answered any of the answers as true ?

1 Answer 1

2

try:

    db.collectionname.find({ $or:[{"customer.answers.BUSI_EXP.answered": true},
{"customer.answers.NEW_HOME.answered": true},
{"customer.answers.VEHICLE.answered": true}});
Sign up to request clarification or add additional context in comments.

3 Comments

Thanks for the answer. I just edited the question. I hadn't asked what i really wanted to.
Is there not a way to iterate over the keys ? This is a small part of the actual collection there are about 80 keys to the answers field
No, you have to specify keys with $or.

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.