1

I'm learning MongoDB on my own. I have a collection with entries that look like this:

{
"_id" : ObjectId("5d0c13fbfdca455311248d6f"),
"borough" : "Brooklyn",
"grades" : 
    [ 
        { "date" : ISODate("2014-04-16T00:00:00Z"), "grade" : "A", "score" : 5 },
        { "date" : ISODate("2013-04-23T00:00:00Z"), "grade" : "B", "score" : 2 },
        { "date" : ISODate("2012-04-24T00:00:00Z"), "grade" : "A", "score" : 5 }
    ],
"name" : "C & C Catering Service",
"restaurant_id" : "40357437"
}

And I want to find all restaurants in Brooklyn with at least one grades.grade of A.

I've figured out the first half of the puzzle:

db.restaurants.find({borough:{$eq:"Brooklyn"}})

But how do I query in the "grades" array for grade A?

2 Answers 2

2

Use dot (.) to access and query nested objects:

db.restaurants.find({'borough':{$eq:"Brooklyn"}, 'grades.grade': 'A'})
Sign up to request clarification or add additional context in comments.

Comments

0
db.restaurants.find({"borough" : "Brooklyn","grades.grade":"A"})

1 Comment

While this code may solve the question, including an explanation really helps to improve the quality of your post.

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.