w3resource

MongoDB Exercise - Find the restaurants in Manhattan that have at least one grade under 5


Write a MongoDB query to find the restaurants that have at least one grade with a score of less than 5 and that are located in the borough of Manhattan.

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.score": { $lt: 5 }, "borough": "Manhattan" })

Output:

{ "_id" : ObjectId("6422c0161238e3bec47ca601"), "address" : { "building" : "351", "coord" : [ -73.98513559999999, 40.7676919 ], "street" : "West   57 Street", "zipcode" : "10019" }, "borough" : "Manhattan", "cuisine" : "Irish", "grades" : [ { "date" : { "$date" : 1409961600000 }, "grade" : "A", "score" : 2 }, { "date" : { "$date" : 1374451200000 }, "grade" : "A", "score" : 11 }, { "date" : { "$date" : 1343692800000 }, "grade" : "A", "score" : 12 }, { "date" : { "$date" : 1325116800000 }, "grade" : "A", "score" : 12 } ], "name" : "Dj Reynolds Pub And Restaurant", "restaurant_id" : "30191841" }
{ "_id" : ObjectId("6422c02c1238e3bec47ca60c"), "address" : { "building" : "1", "coord" : [ -73.96926909999999, 40.7685235 ], "street" : "East   66 Street", "zipcode" : "10065" }, "borough" : "Manhattan", "cuisine" : "American ", "grades" : [ { "date" : { "$date" : 1399420800000 }, "grade" : "A", "score" : 3 }, { "date" : { "$date" : 1367539200000 }, "grade" : "A", "score" : 4 }, { "date" : { "$date" : 1335744000000 }, "grade" : "A", "score" : 6 }, { "date" : { "$date" : 1324944000000 }, "grade" : "A", "score" : 0 } ], "name" : "1 East 66Th Street Kitchen", "restaurant_id" : "40359480" }
{ "_id" : ObjectId("6422c04d1238e3bec47ca618"), "address" : { "building" : "18", "coord" : [ -73.996984, 40.72589 ], "street" : "West Houston Street", "zipcode" : "10012" }, "borough" : "Manhattan", "cuisine" : "American ", "grades" : [ { "date" : { "$date" : 1396483200000 }, "grade" : "A", "score" : 9 }, { "date" : { "$date" : 1365120000000 }, "grade" : "A", "score" : 4 }, { "date" : { "$date" : 1332288000000 }, "grade" : "A", "score" : 13 }, { "date" : { "$date" : 1303862400000 }, "grade" : "A", "score" : 5 } ], "name" : "Angelika Film Center", "restaurant_id" : "40362274" }
.....

Explanation:

The said query in MongoDB that returns all documents in the 'restaurants' collection that have at least one grade with a score of less than 5 and that are located in the borough of Manhattan.

The condition searches for documents where the "grades" field contains at least one sub-document where the "score" field has a value less than 5, and the "borough" field has a value of "Manhattan".

Note: This output is generated using MongoDB server version 3.6

Improve this sample solution and post your code through Disqus.

Previous: Restaurants with less than 5 grades.
Next: Find restaurants in Manhattan or Brooklyn that have at least one grade under 5

What is the difficulty level of this exercise?



Become a Patron!

Follow us on Facebook and Twitter for latest update.

It will be nice if you may share this link in any developer community or anywhere else, from where other developers may find this content. Thanks.

https://www.w3resource.com/mongodb-exercises/mongodb-exercise-34.php