0

I have array of nested objects in my MongoDB schema. Lets say,

var Post = {
    "title": "Some title",
    "advertisers" : [
        {
            "category" : "Tech",
            "name" : "Henry",
            "city" : "New york",
            "updated_at" : ISODate("2011-07-26T21:02:19Z"),
            "created_at" : ISODate("2011-07-26T21:02:19Z")
        },
    ]
}

I want to find the post based on the multiple values in nested schema. eg

Post.find({ 'advertisers.category' : 'Tech', 'advertisers.city': 'New York' }); 

It returns post which matches one of the above criteria. But i want posts which matches both.

How can i do it?

1 Answer 1

2

You can use the $elemMatch operator to do this:

Post.find({ advertisers: {$elemMatch: {category: 'Tech', city: 'New York' }}})
Sign up to request clarification or add additional context in comments.

Comments

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.