MongoDB Exercise - Показать идентификатор ресторана, название и оценки для тех ресторанов, которые возвращают 0 как остаток после деления счета на 7
Напишите запрос MongoDB, который выберет идентификатор ресторана, название и оценки для тех ресторанов, который возвращает 0 в качестве остатка после деления счета на 7.
Структура коллекции ресторанов
{ "адрес": { «здание»: «1007», «координата»: [-73,856077, 40,848447], "улица": "Моррис Парк Авеню", почтовый индекс: 10462 }, "Боро": "Бронкс", "кухня": "пекарня", "оценки": [ {"date": {"$ date": 1393804800000}, "оценка": "A", "оценка": 2}, {"date": {"$ date": 1378857600000}, "оценка": "A", "оценка": 6}, {"date": {"$ date": 1358985600000}, "оценка": "A", "оценка": 10}, {"date": {"$ date": 1322006400000}, "оценка": "A", "оценка": 9}, {"date": {"$ date": 1299715200000}, "оценка": "B", "оценка": 14} ], "name": "Morris Park Bake Shop", "restaurant_id": "30075445" }
Запрос:
db.restaurants.find(
{"grades.score" :
{$mod : [7,0]}
},
{"restaurant_id" : 1,"name":1,"grades":1}
);
Выход:
{"_id": ObjectId ("564c2d939eb21ad392f175ca"), "оценки": [{"дата": ISODate ("2014-03-03T00: 00: 00Z"), "оценка": "А", "оценка": 2 }, {"date": ISODate ("2013-09-11T00: 00: 00Z"), "grade": "A", "score": 6}, {"date": ISODate ("2013-01-24T00 : 00: 00Z ")," оценка ":" A "," оценка ": 10}, {" дата ": ISODate (" 2011-11-23T00: 00: 00Z ")," оценка ":" A ", "оценка": 9}, {"дата": ISODate ("2011-03-10T00: 00: 00Z"), "оценка": "B", "оценка": 14}], "имя": "Моррис Парк Bake Shop "," restaurant_id ":" 30075445 "} {"_id": ObjectId ("564c2d939eb21ad392f175cb"), "оценки": [{"дата": ISODate ("2014-06-10T00: 00: 00Z"), "оценка": "А", "оценка": 5 }, {"date": ISODate ("2013-06-05T00: 00: 00Z"), "grade": "A", "Score": 7}, {"date": ISODate ("2012-04-13T00 : 00: 00Z ")," оценка ":" A "," оценка ": 12}, {" дата ": ISODate (" 2011-10-12T00: 00: 00Z ")," оценка ":" 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 ")," оценка ":" A "," оценка ": 7}, {" дата ": ISODate (" 2012-02-10T00: 00: 00Z ")," оценка ":" 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 ")," оценка ":" C "," оценка ": 56}, {" дата ": ISODate (" 2012-08-15T00: 00: 00Z ")," оценка ":" B ", "оценка": 27}, {"дата": ISODate ("2012-03-28T00: 00: 00Z"), "оценка": "B", "оценка": 27}], "имя": "май май Кухня "," 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 ")," оценка ":" A "," оценка ": 6}, {" дата ": ISODate (" 2011-12-27T00: 00: 00Z ")," оценка ":" 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 ")," оценка ":" A "," оценка ": 7}, {" дата ": ISODate (" 2012-05-21T00: 00: 00Z ")," оценка ":" A ", "оценка": 9}, {"дата": ISODate ("2011-12-30T00: 00: 00Z"), "оценка": "B", "оценка": 19}], "имя": "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 ")," оценка ":" A "," оценка ": 7}, {" дата ": ISODate (" 2011-09-29T00: 00: 00Z ")," оценка ":" 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 ")," оценка ":" A "," оценка ": 8}, {" дата ": ISODate (" 2011-04-05T00: 00: 00Z ")," оценка ":" A ", "score": 7}], "name": "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 ")," оценка ":" A "," оценка ": 13}, {" дата ": ISODate (" 2013-01-09T00: 00: 00Z ")," оценка ":" A ", "оценка": 11}, {"дата": ISODate ("2012-04-10T00: 00: 00Z"), "оценка": "А", "оценка": 10}, {"дата": ISODate (" 2011-11-15T00: 00: 00Z ")," оценка ":" A "," scor e ": 7}]," name ":" Кухня Гарриет "," 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 ")," оценка ":" A "," оценка ": 12}, {" дата ": ISODate (" 2012-11-14T00: 00: 00Z ")," оценка ":" A ", "оценка": 10}, {"дата": ISODate ("2012-10-11T00: 00: 00Z"), "оценка": "P", "оценка": 0}, {"дата": ISODate (" 2012-05-24T00: 00: 00Z ")," оценка ":" А "," оценка " : 13}, {"дата": ISODate ("2011-12-08T00: 00: 00Z"), "оценка": "A", "оценка": 12}, {"дата": ISODate ("2011-07 -20T00: 00: 00Z ")," оценка ":" A "," оценка ": 11}]," name ":" Ho Mei Restaurant "," restaurant_id ":" 40362432 "} {"_id": ObjectId ("564c2d939eb21ad392f175e5"), "оценки": [{"дата": ISODate ("2014-05-13T00: 00: 00Z"), "оценка": "А", "оценка": 2 }, {"date": ISODate ("2013-05-08T00: 00: 00Z"), "grade": "A", "score": 7}, {"date": ISODate ("2012-09-22T00 : 00: 00Z ")," оценка ":" A "," оценка ": 11}, {" дата ": ISODate (" 2011-06-06T00: 00: 00Z ")," оценка ":" 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 ")," оценка ":" A "," оценка ": 7}, {" дата ": ISODate (" 2011-09-22T00: 00: 00Z ")," оценка ":" 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 ")," оценка ":" A "," оценка ": 7}, {" дата ": ISODate (" 2012-04-20T00: 00: 00Z ")," оценка ":" A ", "оценка": 7}, {"дата": ISODate ("2011-12-19T00: 00: 00Z"), "оценка": "А", "оценка": 3}], "имя": "Оливия" , "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 ")," оценка ":" A "," оценка ": 0}, {" дата ": ISODate (" 2011-09-13T00: 00: 00Z ")," оценка ":" A ", "оценка": 12}, {"дата": ISODate ("2011-05-03T00: 00: 00Z"), "оценка": "А", "оценка": 5}], "имя": "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 ")," оценка ":" A "," оценка ": 7}, {" дата ": ISODate (" 2012-03-26T00: 00: 00Z ")," оценка ":" A ", "оценка": 10}, {"дата": ISODate ("2011-11-04T00: 00: 00Z"), "оценка": "А", "оценка": 0}, {"дата": ISODate (" 2011-06-29T00: 00: 00Z ")," оценка ":" А "," оценка " : 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 ")," оценка ":" A "," оценка ": 12}, {" дата ": ISODate (" 2013-01-30T00: 00: 00Z ")," оценка ":" A ", "оценка": 4}, {"дата": ISODate ("2012-01-05T00: 00: 00Z"), "оценка": "А", "оценка": 2}, {"дата": ISODate (" 2011-09-26T00: 00: 00Z ")," оценка ":" А "," оценка " : 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"), "оценки": [{"дата": ISODate ("2014-11-18T00: 00: 00Z"), "оценка": "А", "оценка": 12 }, {"date": ISODate ("2013-11-07T00: 00: 00Z"), "grade": "A", "score": 12}, {"date": ISODate ("2013-04-24T00 : 00: 00Z ")," оценка ":" A "," оценка ": 7}, {" дата ": ISODate (" 2012-03-20T00: 00: 00Z ")," оценка ":" 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 ")," оценка ":" A "," оценка ": 13}, {" дата ": ISODate (" 2012-09-24T00: 00: 00Z ")," оценка ":" A ", "оценка": 8}, {"дата": ISODate ("2012-04-19T00: 00: 00Z"), "оценка": "А", "оценка": 7}], "имя": "Техасский гриль" "," 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 ")," оценка ":" A "," оценка ": 10}, {" дата ": ISODate (" 2011-11-09T00: 00: 00Z ")," оценка ":" A ", "score": 12}], "name": "Philadelhia Grille Express", "restaurant_id": "40364305"} Типа "это" для более
Примечание. Этот вывод генерируется с использованием сервера MongoDB версии 3.6.
Улучшите этот пример решения и опубликуйте свой код через Disqus.
Предыдущий: Напишите запрос MongoDB, который выберет все документы в коллекции ресторанов, где значение поля координат является двойным.
Далее: Напишите запрос MongoDB, чтобы найти название ресторана, район, долготу и отношение, а также кухню для тех ресторанов, в которой где-то в названии есть три буквы «mon».
Каков уровень сложности этого упражнения?
Новый контент: Composer: менеджер зависимостей для PHP , R программирования