MongoDB Exercise - Display restaurant Id, name and grades for those restaurants which returns 0 as a remainder after dividing the score by 7
Write a MongoDB query which will select the restaurant Id, name and grades for those restaurants which returns 0 as a remainder after dividing the score by 7.
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" :
{$mod : [7,0]}
},
{"restaurant_id" : 1,"name":1,"grades":1}
);
Output:
{ "_id" : ObjectId("564c2d939eb21ad392f175ca"), "grades" : [ { "date" : ISODate("2014-03-03T00:00:00Z"), "grade" : "A", "score" : 2 }, { "date" : ISODate("2013-09-11T00:00:00Z"), "grade" : "A", "score" : 6 }, { "date" : ISODate("2013-01-24T00:00:00Z"), "grade" : "A", "score" : 10 }, { "date" : ISODate("2011-11-23T00:00:00Z"), "grade" : "A", "score" : 9 }, { "date" : ISODate("2011-03-10T00:00:00Z"), "grade" : "B", "score" : 14 } ], "name" : "Morris Park Bake Shop", "restaurant_id" : "30075445" } { "_id" : ObjectId("564c2d939eb21ad392f175cb"), "grades" : [ { "date" : ISODate("2014-06-10T00:00:00Z"), "grade" : "A", "score" : 5 }, { "date" : ISODate("2013-06-05T00:00:00Z"), "grade" : "A", "score" : 7 }, { "date" : ISODate("2012-04-13T00:00:00Z"), "grade" : "A", "score" : 12 }, { "date" : ISODate("2011-10-12T00:00:00Z"), "grade" : "A", "score" : 12 } ], "name" : "Riviera Caterer", "restaurant_id" : "40356018" } { "_id" : ObjectId("564c2d939eb21ad392f175ce"), "grades" : [ { "date" : ISODate("2014-11-15T00:00:00Z"), "grade" : "Z", "score" : 38 }, { "date" : ISODate("2014-05-02T00:00:00Z"), "grade" : "A", "score" : 10 }, { "date" : ISODate("2013-03-02T00:00:00Z"), "grade" : "A", "score" : 7 }, { "date" : ISODate("2012-02-10T00:00:00Z"), "grade" : "A", "score" : 13 } ], "name" : "Brunos On The Boulevard", "restaurant_id" : "40356151" } { "_id" : ObjectId("564c2d939eb21ad392f175d4"), "grades" : [ { "date" : ISODate("2014-09-16T00:00:00Z"), "grade" : "B", "score" : 21 }, { "date" : ISODate("2013-08-28T00:00:00Z"), "grade" : "A", "score" : 7 }, { "date" : ISODate("2013-04-02T00:00:00Z"), "grade" : "C", "score" : 56 }, { "date" : ISODate("2012-08-15T00:00:00Z"), "grade" : "B", "score" : 27 }, { "date" : ISODate("2012-03-28T00:00:00Z"), "grade" : "B", "score" : 27 } ], "name" : "May May Kitchen", "restaurant_id" : "40358429" } { "_id" : ObjectId("564c2d939eb21ad392f175d5"), "grades" : [ { "date" : ISODate("2014-05-07T00:00:00Z"), "grade" : "A", "score" : 3 }, { "date" : ISODate("2013-05-03T00:00:00Z"), "grade" : "A", "score" : 4 }, { "date" : ISODate("2012-04-30T00:00:00Z"), "grade" : "A", "score" : 6 }, { "date" : ISODate("2011-12-27T00:00:00Z"), "grade" : "A", "score" : 0 } ], "name" : "1 East 66Th Street Kitchen", "restaurant_id" : "40359480" } { "_id" : ObjectId("564c2d939eb21ad392f175d6"), "grades" : [ { "date" : ISODate("2014-11-10T00:00:00Z"), "grade" : "A", "score" : 11 }, { "date" : ISODate("2013-10-10T00:00:00Z"), "grade" : "A", "score" : 13 }, { "date" : ISODate("2012-10-04T00:00:00Z"), "grade" : "A", "score" : 7 }, { "date" : ISODate("2012-05-21T00:00:00Z"), "grade" : "A", "score" : 9 }, { "date" : ISODate("2011-12-30T00:00:00Z"), "grade" : "B", "score" : 19 } ], "name" : "Seuda Foods", "restaurant_id" : "40360045" } { "_id" : ObjectId("564c2d939eb21ad392f175dc"), "grades" : [ { "date" : ISODate("2014-08-16T00:00:00Z"), "grade" : "A", "score" : 12 }, { "date" : ISODate("2013-08-27T00:00:00Z"), "grade" : "A", "score" : 9 }, { "date" : ISODate("2012-09-20T00:00:00Z"), "grade" : "A", "score" : 7 }, { "date" : ISODate("2011-09-29T00:00:00Z"), "grade" : "A", "score" : 10 } ], "name" : "Sal'S Deli", "restaurant_id" : "40361618" } { "_id" : ObjectId("564c2d939eb21ad392f175df"), "grades" : [ { "date" : ISODate("2014-03-19T00:00:00Z"), "grade" : "A", "score" : 3 }, { "date" : ISODate("2013-03-13T00:00:00Z"), "grade" : "A", "score" : 12 }, { "date" : ISODate("2012-03-27T00:00:00Z"), "grade" : "A", "score" : 8 }, { "date" : ISODate("2011-04-05T00:00:00Z"), "grade" : "A", "score" : 7 } ], "name" : "Steve Chu'S Deli & Grocery", "restaurant_id" : "40361998" } { "_id" : ObjectId("564c2d939eb21ad392f175e0"), "grades" : [ { "date" : ISODate("2014-09-15T00:00:00Z"), "grade" : "A", "score" : 10 }, { "date" : ISODate("2014-03-04T00:00:00Z"), "grade" : "A", "score" : 13 }, { "date" : ISODate("2013-07-18T00:00:00Z"), "grade" : "A", "score" : 13 }, { "date" : ISODate("2013-01-09T00:00:00Z"), "grade" : "A", "score" : 11 }, { "date" : ISODate("2012-04-10T00:00:00Z"), "grade" : "A", "score" : 10 }, { "date" : ISODate("2011-11-15T00:00:00Z"), "grade" : "A", "scor e" : 7 } ], "name" : "Harriet'S Kitchen", "restaurant_id" : "40362098" } { "_id" : ObjectId("564c2d939eb21ad392f175e4"), "grades" : [ { "date" : ISODate("2014-04-21T00:00:00Z"), "grade" : "A", "score" : 10 }, { "date" : ISODate("2013-11-12T00:00:00Z"), "grade" : "A", "score" : 5 }, { "date" : ISODate("2013-06-04T00:00:00Z"), "grade" : "A", "score" : 12 }, { "date" : ISODate("2012-11-14T00:00:00Z"), "grade" : "A", "score" : 10 }, { "date" : ISODate("2012-10-11T00:00:00Z"), "grade" : "P", "score" : 0 }, { "date" : ISODate("2012-05-24T00:00:00Z"), "grade" : "A", "score" : 13 }, { "date" : ISODate("2011-12-08T00:00:00Z"), "grade" : "A", "score" : 12 }, { "date" : ISODate("2011-07-20T00:00:00Z"), "grade" : "A", "score" : 11 } ], "name" : "Ho Mei Restaurant", "restaurant_id" : "40362432" } { "_id" : ObjectId("564c2d939eb21ad392f175e5"), "grades" : [ { "date" : ISODate("2014-05-13T00:00:00Z"), "grade" : "A", "score" : 2 }, { "date" : ISODate("2013-05-08T00:00:00Z"), "grade" : "A", "score" : 7 }, { "date" : ISODate("2012-09-22T00:00:00Z"), "grade" : "A", "score" : 11 }, { "date" : ISODate("2011-06-06T00:00:00Z"), "grade" : "A", "score" : 12 } ], "name" : "Shashemene Int'L Restaura", "restaurant_id" : "40362869" } { "_id" : ObjectId("564c2d939eb21ad392f175e6"), "grades" : [ { "date" : ISODate("2014-03-18T00:00:00Z"), "grade" : "A", "score" : 8 }, { "date" : ISODate("2013-03-18T00:00:00Z"), "grade" : "A", "score" : 8 }, { "date" : ISODate("2012-10-10T00:00:00Z"), "grade" : "A", "score" : 7 }, { "date" : ISODate("2011-09-22T00:00:00Z"), "grade" : "A", "score" : 2 } ], "name" : "White Castle", "restaurant_id" : "40362344" } { "_id" : ObjectId("564c2d939eb21ad392f175eb"), "grades" : [ { "date" : ISODate("2014-10-17T00:00:00Z"), "grade" : "A", "score" : 11 }, { "date" : ISODate("2013-09-18T00:00:00Z"), "grade" : "A", "score" : 13 }, { "date" : ISODate("2013-04-30T00:00:00Z"), "grade" : "A", "score" : 7 }, { "date" : ISODate("2012-04-20T00:00:00Z"), "grade" : "A", "score" : 7 }, { "date" : ISODate("2011-12-19T00:00:00Z"), "grade" : "A", "score" : 3 } ], "name" : "Olive'S", "restaurant_id" : "40363151" } { "_id" : ObjectId("564c2d939eb21ad392f175ee"), "grades" : [ { "date" : ISODate("2014-02-21T00:00:00Z"), "grade" : "A", "score" : 3 }, { "date" : ISODate("2013-09-13T00:00:00Z"), "grade" : "A", "score" : 3 }, { "date" : ISODate("2012-08-28T00:00:00Z"), "grade" : "A", "score" : 0 }, { "date" : ISODate("2011-09-13T00:00:00Z"), "grade" : "A", "score" : 12 }, { "date" : ISODate("2011-05-03T00:00:00Z"), "grade" : "A", "score" : 5 } ], "name" : "Lexler Deli", "restaurant_id" : "40363426" } { "_id" : ObjectId("564c2d939eb21ad392f175f0"), "grades" : [ { "date" : ISODate("2014-02-25T00:00:00Z"), "grade" : "A", "score" : 7 }, { "date" : ISODate("2013-08-14T00:00:00Z"), "grade" : "A", "score" : 11 }, { "date" : ISODate("2012-08-07T00:00:00Z"), "grade" : "A", "score" : 7 }, { "date" : ISODate("2012-03-26T00:00:00Z"), "grade" : "A", "score" : 10 }, { "date" : ISODate("2011-11-04T00:00:00Z"), "grade" : "A", "score" : 0 }, { "date" : ISODate("2011-06-29T00:00:00Z"), "grade" : "A", "score" : 4 } ], "name" : "Snack Time Grill", "restaurant_id" : "40363590" } { "_id" : ObjectId("564c2d939eb21ad392f175f2"), "grades" : [ { "date" : ISODate("2014-08-05T00:00:00Z"), "grade" : "A", "score" : 3 }, { "date" : ISODate("2014-03-06T00:00:00Z"), "grade" : "A", "score" : 11 }, { "date" : ISODate("2013-07-09T00:00:00Z"), "grade" : "A", "score" : 12 }, { "date" : ISODate("2013-01-30T00:00:00Z"), "grade" : "A", "score" : 4 }, { "date" : ISODate("2012-01-05T00:00:00Z"), "grade" : "A", "score" : 2 }, { "date" : ISODate("2011-09-26T00:00:00Z"), "grade" : "A", "score" : 0 } ], "name" : "Domino'S Pizza", "restaurant_id" : "40363644" } { "_id" : ObjectId("564c2d939eb21ad392f175f4"), "grades" : [ { "date" : ISODate("2014-02-05T00:00:00Z"), "grade" : "A", "score" : 0 }, { "date" : ISODate("2013-01-29T00:00:00Z"), "grade" : "A", "score" : 3 }, { "date" : ISODate("2011-12-08T00:00:00Z"), "grade" : "A", "score" : 10 } ], "name" : "Sonny'S Heros", "restaurant_id" : "40363744" } { "_id" : ObjectId("564c2d939eb21ad392f175fb"), "grades" : [ { "date" : ISODate("2014-11-18T00:00:00Z"), "grade" : "A", "score" : 12 }, { "date" : ISODate("2013-11-07T00:00:00Z"), "grade" : "A", "score" : 12 }, { "date" : ISODate("2013-04-24T00:00:00Z"), "grade" : "A", "score" : 7 }, { "date" : ISODate("2012-03-20T00:00:00Z"), "grade" : "A", "score" : 5 } ], "name" : "Plaza Bagels & Deli", "restaurant_id" : "40364286" } { "_id" : ObjectId("564c2d939eb21ad392f175fe"), "grades" : [ { "date" : ISODate("2015-01-06T00:00:00Z"), "grade" : "A", "score" : 12 }, { "date" : ISODate("2014-07-15T00:00:00Z"), "grade" : "C", "score" : 48 }, { "date" : ISODate("2013-05-02T00:00:00Z"), "grade" : "A", "score" : 13 }, { "date" : ISODate("2012-09-24T00:00:00Z"), "grade" : "A", "score" : 8 }, { "date" : ISODate("2012-04-19T00:00:00Z"), "grade" : "A", "score" : 7 } ], "name" : "Texas Rotisserie", "restaurant_id" : "40364304" } { "_id" : ObjectId("564c2d939eb21ad392f175ff"), "grades" : [ { "date" : ISODate("2014-02-25T00:00:00Z"), "grade" : "A", "score" : 12 }, { "date" : ISODate("2013-06-27T00:00:00Z"), "grade" : "A", "score" : 7 }, { "date" : ISODate("2012-12-03T00:00:00Z"), "grade" : "A", "score" : 10 }, { "date" : ISODate("2011-11-09T00:00:00Z"), "grade" : "A", "score" : 12 } ], "name" : "Philadelhia Grille Express", "restaurant_id" : "40364305" } Type "it" for more
Note: This output is generated using MongoDB server version 3.6
Explanation:
The said query in MongoDB that searches for a list of all restaurants where the score field within the grades array is divisible by 7, along with their respective restaurant_id, name, and grades field values.
The "grades.score" mention that the search should be performed on the score field within the grades array and the
$mod : [7,0] search only documents where the score field is divisible by 7 . The $mod operator in MongoDB performs a modulo operation on the specified field value and returns the documents where the remainder is 0.
Improve this sample solution and post your code through Disqus.
Previous: Select all documents in the restaurants collection where the coord field value is double.
Next: Find the restaurant's name, borough, longitude, attitude and cuisine if the restaurant contains three letters 'mon'.
What is the difficulty level of this exercise?
- Weekly Trends and Language Statistics
- Weekly Trends and Language Statistics