w3resource
w3resource logo

MongoDB Exercise - Find the restaurants which do not prepare any cuisine of American and achieved a grade point A not belongs to the borough Brooklyn


Write a MongoDB query to find the restaurants which do not prepare any cuisine of 'American ' and achieved a grade point 'A' not belongs to the borough Brooklyn.
The document must be displayed according to the cuisine in descending order.

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( {
                             "cuisine" : {$ne : "American "},
                             "grades.grade" :"A",
                             "borough": "Brooklyn"
                       } 
                    ).sort({"cuisine":-1});

Output:

{ "_id" : ObjectId("5ab23ff163b071e80805d549"), "address" : { "building" : "4222", "coord" : [ -73.9996977, 40.6452763 ], "street" : "8 Avenue", "zipcode" : "11232" }, "borough" : "Brooklyn", "cuisine
" : "Vietnamese/Cambodian/Malaysia", "grades" : [ { "date" : ISODate("2014-08-19T00:00:00Z"), "grade" : "B", "score" : 17 }, { "date" : ISODate("2014-03-07T00:00:00Z"), "grade" : "B", "score" : 17 },
{ "date" : ISODate("2013-02-01T00:00:00Z"), "grade" : "A", "score" : 12 }, { "date" : ISODate("2012-01-26T00:00:00Z"), "grade" : "A", "score" : 12 }, { "date" : ISODate("2011-12-15T00:00:00Z"), "grade
" : "P", "score" : 9 } ], "name" : "Ba Xuyen", "restaurant_id" : "40959591" }
{ "_id" : ObjectId("5ab23ff163b071e80805dc71"), "address" : { "building" : "1243", "coord" : [ -73.95861599999999, 40.5987922 ], "street" : "Avenue U", "zipcode" : "11229" }, "borough" : "Brooklyn", "
cuisine" : "Vietnamese/Cambodian/Malaysia", "grades" : [ { "date" : ISODate("2014-10-06T00:00:00Z"), "grade" : "B", "score" : 27 }, { "date" : ISODate("2014-02-18T00:00:00Z"), "grade" : "B", "score" :
 26 }, { "date" : ISODate("2013-08-01T00:00:00Z"), "grade" : "A", "score" : 12 }, { "date" : ISODate("2012-07-23T00:00:00Z"), "grade" : "A", "score" : 9 }, { "date" : ISODate("2012-03-17T00:00:00Z"),
"grade" : "A", "score" : 10 } ], "name" : "Pho Viet-Nam Restaurant", "restaurant_id" : "41158964" }
{ "_id" : ObjectId("5ab23ff163b071e80805e705"), "address" : { "building" : "6008", "coord" : [ -74.01218240000001, 40.636655 ], "street" : "7 Avenue", "zipcode" : "11220" }, "borough" : "Brooklyn", "c
uisine" : "Vietnamese/Cambodian/Malaysia", "grades" : [ { "date" : ISODate("2014-12-10T00:00:00Z"), "grade" : "B", "score" : 17 }, { "date" : ISODate("2014-05-14T00:00:00Z"), "grade" : "A", "score" :
6 }, { "date" : ISODate("2013-04-16T00:00:00Z"), "grade" : "A", "score" : 12 }, { "date" : ISODate("2012-04-26T00:00:00Z"), "grade" : "A", "score" : 12 }, { "date" : ISODate("2011-12-12T00:00:00Z"), "
grade" : "A", "score" : 2 } ], "name" : "Thanh Da", "restaurant_id" : "41353290" }
{ "_id" : ObjectId("5ab23ff163b071e80805ef29"), "address" : { "building" : "5414", "coord" : [ -74.0066298, 40.63871049999999 ], "street" : "8 Avenue", "zipcode" : "11220" }, "borough" : "Brooklyn", "
cuisine" : "Vietnamese/Cambodian/Malaysia", "grades" : [ { "date" : ISODate("2014-11-19T00:00:00Z"), "grade" : "A", "score" : 9 }, { "date" : ISODate("2014-04-24T00:00:00Z"), "grade" : "B", "score" :
26 }, { "date" : ISODate("2013-10-24T00:00:00Z"), "grade" : "A", "score" : 11 }, { "date" : ISODate("2013-03-20T00:00:00Z"), "grade" : "B", "score" : 22 }, { "date" : ISODate("2011-12-20T00:00:00Z"),
"grade" : "A", "score" : 12 } ], "name" : "New Gia Lam Vietnamese Food", "restaurant_id" : "41457893" }
{ "_id" : ObjectId("5ab23ff163b071e80805ef78"), "address" : { "building" : "349", "coord" : [ -73.98379, 40.6717439 ], "street" : "5 Avenue", "zipcode" : "11215" }, "borough" : "Brooklyn", "cuisine" :
 "Vietnamese/Cambodian/Malaysia", "grades" : [ { "date" : ISODate("2014-09-19T00:00:00Z"), "grade" : "A", "score" : 12 }, { "date" : ISODate("2014-04-09T00:00:00Z"), "grade" : "A", "score" : 9 }, { "d
ate" : ISODate("2013-02-20T00:00:00Z"), "grade" : "A", "score" : 5 }, { "date" : ISODate("2012-01-30T00:00:00Z"), "grade" : "A", "score" : 6 } ], "name" : "Home Vietnamese Sandwich And Bubble Tea", "r
estaurant_id" : "41461382" }
{ "_id" : ObjectId("5ab23ff163b071e80805f1d2"), "address" : { "building" : "229", "coord" : [ -73.9807822, 40.6754643 ], "street" : "5 Avenue", "zipcode" : "11215" }, "borough" : "Brooklyn", "cuisine"
 : "Vietnamese/Cambodian/Malaysia", "grades" : [ { "date" : ISODate("2014-03-12T00:00:00Z"), "grade" : "A", "score" : 10 }, { "date" : ISODate("2013-09-26T00:00:00Z"), "grade" : "A", "score" : 9 }, {
"date" : ISODate("2012-08-29T00:00:00Z"), "grade" : "A", "score" : 12 }, { "date" : ISODate("2012-03-29T00:00:00Z"), "grade" : "A", "score" : 9 }, { "date" : ISODate("2011-10-27T00:00:00Z"), "grade" :
 "A", "score" : 10 }, { "date" : ISODate("2011-05-12T00:00:00Z"), "grade" : "A", "score" : 10 } ], "name" : "Pink Lotus Gourmet", "restaurant_id" : "41486961" }
{ "_id" : ObjectId("5ab23ff163b071e80805f49d"), "address" : { "building" : "109", "coord" : [ -73.9452471, 40.7072842 ], "street" : "Montrose Avenue", "zipcode" : "11206" }, "borough" : "Brooklyn", "c
uisine" : "Vietnamese/Cambodian/Malaysia", "grades" : [ { "date" : ISODate("2014-03-04T00:00:00Z"), "grade" : "A", "score" : 8 }, { "date" : ISODate("2013-09-23T00:00:00Z"), "grade" : "A", "score" : 9
 }, { "date" : ISODate("2013-04-05T00:00:00Z"), "grade" : "A", "score" : 5 }, { "date" : ISODate("2011-12-15T00:00:00Z"), "grade" : "A", "score" : 10 }, { "date" : ISODate("2011-10-13T00:00:00Z"), "gr
ade" : "P", "score" : 3 }, { "date" : ISODate("2011-03-30T00:00:00Z"), "grade" : "B", "score" : 22 } ], "name" : "Nam Nam", "restaurant_id" : "41528836" }
{ "_id" : ObjectId("5ab23ff163b071e80805fbc9"), "address" : { "building" : "433", "coord" : [ -73.9841959, 40.6633945 ], "street" : "7 Avenue", "zipcode" : "11215" }, "borough" : "Brooklyn", "cuisine"
 : "Vietnamese/Cambodian/Malaysia", "grades" : [ { "date" : ISODate("2014-04-30T00:00:00Z"), "grade" : "A", "score" : 9 }, { "date" : ISODate("2013-11-20T00:00:00Z"), "grade" : "A", "score" : 10 }, {
"date" : ISODate("2013-04-24T00:00:00Z"), "grade" : "A", "score" : 13 }, { "date" : ISODate("2012-02-25T00:00:00Z"), "grade" : "A", "score" : 9 }, { "date" : ISODate("2011-08-26T00:00:00Z"), "grade" :
 "A", "score" : 4 } ], "name" : "Henry", "restaurant_id" : "41604359" }
{ "_id" : ObjectId("5ab23ff163b071e80805fc8b"), "address" : { "building" : "685", "coord" : [ -73.95178659999999, 40.7250093 ], "street" : "Manhattan Avenue", "zipcode" : "11222" }, "borough" : "Brook
lyn", "cuisine" : "Vietnamese/Cambodian/Malaysia", "grades" : [ { "date" : ISODate("2014-12-31T00:00:00Z"), "grade" : "Z", "score" : 25 }, { "date" : ISODate("2014-07-23T00:00:00Z"), "grade" : "C", "s
core" : 4 }, { "date" : ISODate("2014-02-13T00:00:00Z"), "grade" : "A", "score" : 12 }, { "date" : ISODate("2013-07-10T00:00:00Z"), "grade" : "A", "score" : 11 }, { "date" : ISODate("2013-01-14T00:00:
00Z"), "grade" : "A", "score" : 12 } ], "name" : "A+ Lollipop", "restaurant_id" : "41611605" }
{ "_id" : ObjectId("5ab23ff163b071e808060215"), "address" : { "building" : "263", "coord" : [ -73.9808238, 40.6055108 ], "street" : "Kings Highway", "zipcode" : "11223" }, "borough" : "Brooklyn", "cui
sine" : "Vietnamese/Cambodian/Malaysia", "grades" : [ { "date" : ISODate("2014-08-14T00:00:00Z"), "grade" : "A", "score" : 11 }, { "date" : ISODate("2014-01-30T00:00:00Z"), "grade" : "B", "score" : 25
 }, { "date" : ISODate("2013-07-05T00:00:00Z"), "grade" : "B", "score" : 27 } ], "name" : "Mekong Brooklyn", "restaurant_id" : "41648717" }
{ "_id" : ObjectId("5ab23ff163b071e808060301"), "address" : { "building" : "67", "coord" : [ -73.9658486, 40.7113666 ], "street" : "South    6 Street", "zipcode" : "11249" }, "borough" : "Brooklyn", "
cuisine" : "Vietnamese/Cambodian/Malaysia", "grades" : [ { "date" : ISODate("2014-07-17T00:00:00Z"), "grade" : "B", "score" : 22 }, { "date" : ISODate("2013-12-19T00:00:00Z"), "grade" : "A", "score" :
 12 }, { "date" : ISODate("2013-05-10T00:00:00Z"), "grade" : "A", "score" : 13 }, { "date" : ISODate("2012-09-07T00:00:00Z"), "grade" : "A", "score" : 7 } ], "name" : "Bia Bar & Grill", "restaurant_id
" : "41655832" }
{ "_id" : ObjectId("5ab23ff163b071e80806030f"), "address" : { "building" : "5624", "coord" : [ -74.007891, 40.637407 ], "street" : "8 Avenue", "zipcode" : "11220" }, "borough" : "Brooklyn", "cuisine"
: "Vietnamese/Cambodian/Malaysia", "grades" : [ { "date" : ISODate("2014-05-29T00:00:00Z"), "grade" : "A", "score" : 12 }, { "date" : ISODate("2013-05-13T00:00:00Z"), "grade" : "A", "score" : 12 }, {
"date" : ISODate("2012-05-23T00:00:00Z"), "grade" : "A", "score" : 10 } ], "name" : "Little Thanh Da", "restaurant_id" : "41656294" }
{ "_id" : ObjectId("5ab23ff163b071e8080607eb"), "address" : { "building" : "4317", "coord" : [ -73.9999631, 40.64449339999999 ], "street" : "8 Avenue", "zipcode" : "11232" }, "borough" : "Brooklyn", "
cuisine" : "Vietnamese/Cambodian/Malaysia", "grades" : [ { "date" : ISODate("2014-04-30T00:00:00Z"), "grade" : "B", "score" : 18 }, { "date" : ISODate("2013-03-06T00:00:00Z"), "grade" : "A", "score" :
 12 } ], "name" : "Pho Viet", "restaurant_id" : "41695857" }
{ "_id" : ObjectId("5ab23ff163b071e808060d90"), "address" : { "building" : "112", "coord" : [ -73.928732, 40.70662 ], "street" : "Harrison Place", "zipcode" : "11237" }, "borough" : "Brooklyn", "cuisi
ne" : "Vietnamese/Cambodian/Malaysia", "grades" : [ { "date" : ISODate("2014-02-26T00:00:00Z"), "grade" : "A", "score" : 12 }, { "date" : ISODate("2013-08-07T00:00:00Z"), "grade" : "A", "score" : 10 }
 ], "name" : "Falansai", "restaurant_id" : "50000024" }
{ "_id" : ObjectId("5ab23ff163b071e808060f40"), "address" : { "building" : "2351", "coord" : [ -73.9901162, 40.6000544 ], "street" : "86 Street", "zipcode" : "11214" }, "borough" : "Brooklyn", "cuisin
e" : "Vietnamese/Cambodian/Malaysia", "grades" : [ { "date" : ISODate("2014-05-29T00:00:00Z"), "grade" : "A", "score" : 11 }, { "date" : ISODate("2013-05-28T00:00:00Z"), "grade" : "A", "score" : 11 }
], "name" : "Pho Tay Ho 86 Vietnamese Restaurant", "restaurant_id" : "50000814" }
{ "_id" : ObjectId("5ab23ff163b071e808061191"), "address" : { "building" : "134", "coord" : [ -73.99041, 40.6871561 ], "street" : "Smith Street", "zipcode" : "11201" }, "borough" : "Brooklyn", "cuisin
e" : "Vietnamese/Cambodian/Malaysia", "grades" : [ { "date" : ISODate("2014-10-07T00:00:00Z"), "grade" : "A", "score" : 12 }, { "date" : ISODate("2013-10-15T00:00:00Z"), "grade" : "A", "score" : 11 }
], "name" : "Hanco'S", "restaurant_id" : "50001823" }
{ "_id" : ObjectId("5ab23ff163b071e80806160b"), "address" : { "building" : "6303", "coord" : [ -74.01134309999999, 40.6337446 ], "street" : "8 Avenue", "zipcode" : "11220" }, "borough" : "Brooklyn", "
cuisine" : "Vietnamese/Cambodian/Malaysia", "grades" : [ { "date" : ISODate("2014-03-04T00:00:00Z"), "grade" : "A", "score" : 12 } ], "name" : "Phos & Banh Mi", "restaurant_id" : "50004612" }
{ "_id" : ObjectId("5ab23ff163b071e808061660"), "address" : { "building" : "580", "coord" : [ -73.948588, 40.710921 ], "street" : "Grand Street", "zipcode" : "11211" }, "borough" : "Brooklyn", "cuisin
e" : "Vietnamese/Cambodian/Malaysia", "grades" : [ { "date" : ISODate("2014-09-02T00:00:00Z"), "grade" : "A", "score" : 12 }, { "date" : ISODate("2014-01-30T00:00:00Z"), "grade" : "A", "score" : 7 } ]
, "name" : "Pho", "restaurant_id" : "50004839" }
{ "_id" : ObjectId("5ab23ff163b071e808061965"), "address" : { "building" : "5906", "coord" : [ -74.00937689999999, 40.636024 ], "street" : "8Th Ave", "zipcode" : "11220" }, "borough" : "Brooklyn", "cu
isine" : "Vietnamese/Cambodian/Malaysia", "grades" : [ { "date" : ISODate("2014-10-28T00:00:00Z"), "grade" : "A", "score" : 5 }, { "date" : ISODate("2014-04-14T00:00:00Z"), "grade" : "B", "score" : 23
 } ], "name" : "New Saigon", "restaurant_id" : "50007274" }
{ "_id" : ObjectId("5ab23ff163b071e80805cccd"), "address" : { "building" : "2268", "coord" : [ -73.9564939, 40.650368 ], "street" : "Church Avenue", "zipcode" : "11226" }, "borough" : "Brooklyn", "cui
sine" : "Vegetarian", "grades" : [ { "date" : ISODate("2014-07-28T00:00:00Z"), "grade" : "A", "score" : 10 }, { "date" : ISODate("2014-02-25T00:00:00Z"), "grade" : "A", "score" : 5 }, { "date" : ISODa
te("2013-06-01T00:00:00Z"), "grade" : "A", "score" : 7 }, { "date" : ISODate("2012-10-16T00:00:00Z"), "grade" : "C", "score" : 28 }, { "date" : ISODate("2011-10-06T00:00:00Z"), "grade" : "A", "score"
: 10 } ], "name" : "Strictly Vegetarian", "restaurant_id" : "40587626" }
Type "it" for more
>ISODate("2012-09-05T00:00:00Z"), "grade" : "B", "score
" : 26 }, { "date" : ISODate("2012-04-10T00:00:00Z"), "grade" : "C", "score" : 49 }, { "date" : ISODate("2011-11-21T00:00:00Z"), "grade" : "C", "score" : 80 } ], "name" : "B.B. Kings", "restaurant_id"
 : "40704853" }
{ "_id" : ObjectId("5ab23ff163b071e80805cfad"), "address" : { "building" : "231", "coord" : [ -73.9772294, 40.7527262 ], "street" : "Grand Central Station", "zipcode" : "10017" }, "borough" : "Manhatt
an", "cuisine" : "Italian", "grades" : [ { "date" : ISODate("2015-01-07T00:00:00Z"), "grade" : "Z", "score" : 20 }, { "date" : ISODate("2014-07-03T00:00:00Z"), "grade" : "B", "score" : 14 }, { "date"
: ISODate("2013-12-21T00:00:00Z"), "grade" : "A", "score" : 13 }, { "date" : ISODate("2013-05-17T00:00:00Z"), "grade" : "C", "score" : 76 }, { "date" : ISODate("2012-04-20T00:00:00Z"), "grade" : "A",
"score" : 12 } ], "name" : "Two Boots Grand Central", "restaurant_id" : "40725591" }
{ "_id" : ObjectId("5ab23ff163b071e80805d095"), "address" : { "building" : "", "coord" : [ -74.0163793, 40.7167671 ], "street" : "Hudson River", "zipcode" : "10282" }, "borough" : "Manhattan", "cuisin
e" : "American", "grades" : [ { "date" : ISODate("2014-06-27T00:00:00Z"), "grade" : "C", "score" : 89 }, { "date" : ISODate("2013-06-06T00:00:00Z"), "grade" : "A", "score" : 6 }, { "date" : ISODate("2
012-06-19T00:00:00Z"), "grade" : "A", "score" : 13 } ], "name" : "West 79Th Street Boat Basin Cafe", "restaurant_id" : "40756344" }
{ "_id" : ObjectId("5ab23ff163b071e80805d5d3"), "address" : { "building" : "7705", "coord" : [ -73.8878704, 40.7435875 ], "street" : "Woodside Avenue", "zipcode" : "11373" }, "borough" : "Queens", "cu
isine" : "Thai", "grades" : [ { "date" : ISODate("2014-05-12T00:00:00Z"), "grade" : "B", "score" : 14 }, { "date" : ISODate("2013-11-20T00:00:00Z"), "grade" : "C", "score" : 84 }, { "date" : ISODate("
2012-09-18T00:00:00Z"), "grade" : "A", "score" : 11 }, { "date" : ISODate("2012-05-03T00:00:00Z"), "grade" : "B", "score" : 23 } ], "name" : "Spicy Shallot", "restaurant_id" : "40979431" }
{ "_id" : ObjectId("5ab23ff163b071e80805d607"), "address" : { "building" : "466", "coord" : [ -73.9747277, 40.7536114 ], "street" : "Lexington Avenue", "zipcode
: 10 } ], "name" : "Strictly Vegetarian", "restaurant_id" : "40587626" }
Type "it" for more

Note: This output is generated using MongoDB server version 3.6

Improve this sample solution and post your code through Disqus.