w3resource

MongoDB Exercise - Find the restaurants with a grade of 2 and a grade of 6


Write a MongoDB query to find the restaurants that have a grade with a score of 2 and a grade with a score of 6.

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({
  $and: [
    {"grades.score": 2},
    {"grades.score": 6}
  ]
})

Output:

{ "_id" : ObjectId("6422c0161238e3bec47ca5ff"), "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" }
{ "_id" : ObjectId("6422c7461238e3bec47ca63f"), "address" : { "building" : "261", "coord" : [ -73.94839189999999, 40.7224876 ], "street" : "Driggs Avenue", "zipcode" : "11222" }, "borough" : "Brooklyn", "cuisine" : "Polish", "grades" : [ { "date" : { "$date" : 1401494400000 }, "grade" : "A", "score" : 2 }, { "date" : { "$date" : 1368144000000 }, "grade" : "A", "score" : 3 }, { "date" : { "$date" : 1329436800000 }, "grade" : "A", "score" : 6 }, { "date" : { "$date" : 1318550400000 }, "grade" : "C", "score" : 54 } ], "name" : "Polish National Home", "restaurant_id" : "40364404" }
...

Explanation:

The given query in MongoDB that only returns documents where at least one grades element has a score of 2 and another grades element has a score of 6.

The $and operator combines two separate queries that check for each score. The first one checks where any of the grades array elements have a score field equal to 2 and the second one have a score field equal to 6.

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 in Manhattan or Brooklyn whose cuisine is not American or Chinese, and which have at least one grade lower than 5.
Next: The restaurants in Manhattan with grades of 2 and 6 are listed below.

What is the difficulty level of this exercise?



Follow us on Facebook and Twitter for latest update.