w3resource

MongoDB Exercise - Find restaurants in Manhattan or Brooklyn that have at least one grade above 5


Write a MongoDB query to find the restaurants that have all grades with a score greater than 5 and are located in the borough of Manhattan or Brooklyn.

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({
  "borough": {
    "$in": ["Manhattan", "Brooklyn"]
  },
  "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("6422c0161238e3bec47ca606"), "address" : { "building" : "7114", "coord" : [ -73.9068506, 40.6199034 ], "street" : "Avenue U", "zipcode" : "11234" }, "borough" : "Brooklyn", "cuisine" : "Delicatessen", "grades" : [ { "date" : { "$date" : 1401321600000 }, "grade" : "A", "score" : 10 }, { "date" : { "$date" : 1389657600000 }, "grade" : "A", "score" : 10 }, { "date" : { "$date" : 1375488000000 }, "grade" : "A", "score" : 8 }, { "date" : { "$date" : 1342569600000 }, "grade" : "A", "score" : 10 }, { "date" : { "$date" : 1331251200000 }, "grade" : "A", "score" : 13 }, { "date" : { "$date" : 1318550400000 }, "grade" : "A", "score" : 9 } ], "name" : "Wilken'S Fine Food", "restaurant_id" : "40356483" }

.....

Explanation:

The said query in MongoDB that finds the restaurants that have all grades with a score greater than 5 and are located in the borough of Manhattan or Brooklyn.

.The "$in" operator is used to search for restaurants located in the boroughs of Manhattan or Brooklyn.

The "grades" field is used to find restaurants that have all grades with a score greater than 5.

The implicit "AND" operator is used to combine the two conditions to find all documents that meet both criteria.

Note: This output is generated using MongoDB server version 3.6

Improve this sample solution and post your code through Disqus.

Previous: Find restaurants with grades greater than 5 and located in the boroughManhattan.
Next: Find the average score for each restaurant.

What is the difficulty level of this exercise?



Follow us on Facebook and Twitter for latest update.