w3resource

MongoDB Exercise - All restaurants with a score of 5 or higher


Write a MongoDB query to find the restaurants that have all grades with a score greater than 5.

Structure of 'restaurants' collection :

{
  "address": {
     "building": "1007",
     "coord": [ -73.856077, 40.848447 ],
     "street": "Morris Park Ave",
     "zipcode": "10462"
  },
  "borough": "Bronx",
  "cuisine": "Bakery",
  "grades": [
     { "date": { "$date": 1393804800000 }, "grade": "A", "score": 2 },
     { "date": { "$date": 1378857600000 }, "grade": "A", "score": 6 },
     { "date": { "$date": 1358985600000 }, "grade": "A", "score": 10 },
     { "date": { "$date": 1322006400000 }, "grade": "A", "score": 9 },
     { "date": { "$date": 1299715200000 }, "grade": "B", "score": 14 }
  ],
  "name": "Morris Park Bake Shop",
  "restaurant_id": "30075445"
}

Query:

db.restaurants.find({
  "grades": {
    "$not": {
      "$elemMatch": {
        "score": {
          "$lte": 5
        }
      }
    }
  }
})

Output:

{ "_id" : ObjectId("6422c0161238e3bec47ca600"), "address" : { "building" : "469", "coord" : [ -73.961704, 40.662942 ], "street" : "Flatbush Avenue", "zipcode" : "11225" }, "borough" : "Brooklyn", "cuisine" : "Hamburgers", "grades" : [ { "date" : { "$date" : 1419897600000 }, "grade" : "A", "score" : 8 }, { "date" : { "$date" : 1404172800000 }, "grade" : "B", "score" : 23 }, { "date" : { "$date" : 1367280000000 }, "grade" : "A", "score" : 12 }, { "date" : { "$date" : 1336435200000 }, "grade" : "A", "score" : 12 } ], "name" : "Wendy'S", "restaurant_id" : "30112340" }
{ "_id" : ObjectId("6422c0161238e3bec47ca603"), "address" : { "building" : "97-22", "coord" : [ -73.8601152, 40.7311739 ], "street" : "63 Road", "zipcode" : "11374" }, "borough" : "Queens", "cuisine" : "Jewish/Kosher", "grades" : [ { "date" : { "$date" : 1416787200000 }, "grade" : "Z", "score" : 20 }, { "date" : { "$date" : 1358380800000 }, "grade" : "A", "score" : 13 }, { "date" : { "$date" : 1343865600000 }, "grade" : "A", "score" : 13 }, { "date" : { "$date" : 1323907200000 }, "grade" : "B", "score" : 25 } ], "name" : "Tov Kosher Kitchen", "restaurant_id" : "40356068" }

.....

Explanation:

The given query in MongoDB that finds the restaurants that have all grades with a score greater than 5.

The "$elemMatch" operator is used to match documents that contain an array element that matches the condition which is that the "score" field of the array element must be less than or equal to 5.

The "$not" operator is used to negate the condition that all documents where none of the array elements have a score less than or equal to 5.

Note: This output is generated using MongoDB server version 3.6

Improve this sample solution and post your code through Disqus.

Previous: Look for restaurants with a grade of 2 or 6 that are located in Manhattan or Brooklyn, and whose cuisine isn't American or Chinese.
Next: Find restaurants with grades greater than 5 and located in the boroughManhattan.

What is the difficulty level of this exercise?



Follow us on Facebook and Twitter for latest update.