w3resource
ankara escort
malatya escort

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": {$ne : "Brooklyn"}
                       } 
                    ).sort({"cuisine":-1});

Output:

{ "_id" : ObjectId("55c3043ab165fa6355ec6395"), "address" : { "building" : "89", "coord" : [ -73.9995899, 40.7168015 ], "street" : "Baxter Street", "zipcode" : "10013" },
sine" : "Vietnamese/Cambodian/Malaysia", "grades" : [ { "date" : ISODate("2014-08-21T00:00:00Z"), "grade" : "A", "score" : 13 }, { "date" : ISODate("2013-08-31T00:00:00Z"
 }, { "date" : ISODate("2013-04-11T00:00:00Z"), "grade" : "C", "score" : 3 }, { "date" : ISODate("2012-10-17T00:00:00Z"), "grade" : "A", "score" : 4 }, { "date" : ISODate
de" : "A", "score" : 10 } ], "name" : "Thai Son", "restaurant_id" : "40559606" }
{ "_id" : ObjectId("55c3043ab165fa6355ec644e"), "address" : { "building" : "8278", "coord" : [ -73.88143509999999, 40.7412552 ], "street" : "Broadway", "zipcode" : "11373
isine" : "Vietnamese/Cambodian/Malaysia", "grades" : [ { "date" : ISODate("2014-06-12T00:00:00Z"), "grade" : "B", "score" : 21 }, { "date" : ISODate("2013-05-20T00:00:00Z
3 }, { "date" : ISODate("2012-12-26T00:00:00Z"), "grade" : "A", "score" : 10 }, { "date" : ISODate("2012-12-03T00:00:00Z"), "grade" : "P", "score" : 5 }, { "date" : ISODa
rade" : "B", "score" : 27 } ], "name" : "Pho Bac Vietnamese Seafood Cuisine", "restaurant_id" : "40578058" }
{ "_id" : ObjectId("55c3043ab165fa6355ec6837"), "address" : { "building" : "148", "coord" : [ -74.000254, 40.7172727 ], "street" : "Centre Street", "zipcode" : "10013" },
sine" : "Vietnamese/Cambodian/Malaysia", "grades" : [ { "date" : ISODate("2014-10-01T00:00:00Z"), "grade" : "A", "score" : 7 }, { "date" : ISODate("2014-05-19T00:00:00Z")
, { "date" : ISODate("2013-11-15T00:00:00Z"), "grade" : "B", "score" : 14 }, { "date" : ISODate("2013-03-08T00:00:00Z"), "grade" : "B", "score" : 25 }, { "date" : ISODate
de" : "B", "score" : 23 }, { "date" : ISODate("2011-10-27T00:00:00Z"), "grade" : "A", "score" : 9 } ], "name" : "Nha-Trang Centre Vietnam Restaurant", "restaurant_id" : "
{ "_id" : ObjectId("55c3043ab165fa6355ec6b4e"), "address" : { "building" : "158", "coord" : [ -74.0025903, 40.7443077 ], "street" : "9 Avenue", "zipcode" : "10011" }, "bo
" : "Vietnamese/Cambodian/Malaysia", "grades" : [ { "date" : ISODate("2014-07-15T00:00:00Z"), "grade" : "A", "score" : 11 }, { "date" : ISODate("2014-01-14T00:00:00Z"), "
 "date" : ISODate("2013-04-16T00:00:00Z"), "grade" : "B", "score" : 18 }, { "date" : ISODate("2012-02-15T00:00:00Z"), "grade" : "A", "score" : 7 } ], "name" : "O-Mai", "r
{ "_id" : ObjectId("55c3043ab165fa6355ec6ddf"), "address" : { "building" : "87", "coord" : [ -73.99961909999999, 40.7167408 ], "street" : "Baxter Street", "zipcode" : "10
", "cuisine" : "Vietnamese/Cambodian/Malaysia", "grades" : [ { "date" : ISODate("2015-01-06T00:00:00Z"), "grade" : "A", "score" : 11 }, { "date" : ISODate("2014-01-23T00:
e" : 10 }, { "date" : ISODate("2013-01-08T00:00:00Z"), "grade" : "A", "score" : 13 }, { "date" : ISODate("2012-06-05T00:00:00Z"), "grade" : "A", "score" : 10 } ], "name"
taurant_id" : "40989220" }
{ "_id" : ObjectId("55c3043ab165fa6355ec6e84"), "address" : { "building" : "80", "coord" : [ -73.99862900000001, 40.7158191 ], "street" : "Bayard Street", "zipcode" : "10
", "cuisine" : "Vietnamese/Cambodian/Malaysia", "grades" : [ { "date" : ISODate("2014-11-07T00:00:00Z"), "grade" : "B", "score" : 27 }, { "date" : ISODate("2014-07-05T00:
e" : 27 }, { "date" : ISODate("2013-10-29T00:00:00Z"), "grade" : "A", "score" : 2 }, { "date" : ISODate("2013-06-28T00:00:00Z"), "grade" : "C", "score" : 5 }, { "date" :
), "grade" : "B", "score" : 14 }, { "date" : ISODate("2012-07-18T00:00:00Z"), "grade" : "C", "score" : 42 }, { "date" : ISODate("2012-02-10T00:00:00Z"), "grade" : "A", "s
Bo Ky Restaurant", "restaurant_id" : "41005330" }
{ "_id" : ObjectId("55c3043ab165fa6355ec6f4b"), "address" : { "building" : "156-32", "coord" : [ -73.8087286, 40.7639199 ], "street" : "Northern Boulevard", "zipcode" : "
, "cuisine" : "Vietnamese/Cambodian/Malaysia", "grades" : [ { "date" : ISODate("2014-09-11T00:00:00Z"), "grade" : "A", "score" : 13 }, { "date" : ISODate("2014-04-08T00:0
" : 11 }, { "date" : ISODate("2013-10-07T00:00:00Z"), "grade" : "A", "score" : 12 }, { "date" : ISODate("2012-09-13T00:00:00Z"), "grade" : "A", "score" : 2 }, { "date" :
), "grade" : "A", "score" : 7 }, { "date" : ISODate("2011-11-09T00:00:00Z"), "grade" : "A", "score" : 10 } ], "name" : "Mekong Thai And Vietnamese Restaurant", "restauran
{ "_id" : ObjectId("55c3043ab165fa6355ec6f69"), "address" : { "building" : "2", "coord" : [ -73.9862038, 40.747189 ], "street" : "West   32 Street", "zipcode" : "10001" }
isine" : "Vietnamese/Cambodian/Malaysia", "grades" : [ { "date" : ISODate("2014-12-01T00:00:00Z"), "grade" : "Z", "score" : 18 }, { "date" : ISODate("2014-05-27T00:00:00Z
2 }, { "date" : ISODate("2013-10-01T00:00:00Z"), "grade" : "B", "score" : 17 }, { "date" : ISODate("2012-09-21T00:00:00Z"), "grade" : "A", "score" : 7 }, { "date" : ISODa
rade" : "A", "score" : 13 } ], "name" : "Pho 32 & Shabu", "restaurant_id" : "41026002" }
{ "_id" : ObjectId("55c3043ab165fa6355ec7091"), "address" : { "building" : "345", "coord" : [ -74.0102595, 40.7184972 ], "street" : "Greenwich Street", "zipcode" : "10013
"cuisine" : "Vietnamese/Cambodian/Malaysia", "grades" : [ { "date" : ISODate("2014-08-22T00:00:00Z"), "grade" : "A", "score" : 9 }, { "date" : ISODate("2014-03-15T00:00:0
 18 }, { "date" : ISODate("2012-10-17T00:00:00Z"), "grade" : "A", "score" : 10 }, { "date" : ISODate("2012-05-08T00:00:00Z"), "grade" : "B", "score" : 19 }, { "date" : IS
 "grade" : "A", "score" : 12 } ], "name" : "Viet Cafe", "restaurant_id" : "41063946" }
{ "_id" : ObjectId("55c3043ab165fa6355ec7384"), "address" : { "building" : "4107", "coord" : [ -73.8293161, 40.7583823 ], "street" : "Kissena Boulevard", "zipcode" : "113
cuisine" : "Vietnamese/Cambodian/Malaysia", "grades" : [ { "date" : ISODate("2014-11-13T00:00:00Z"), "grade" : "A", "score" : 10 }, { "date" : ISODate("2014-05-20T00:00:0
 8 }, { "date" : ISODate("2013-10-16T00:00:00Z"), "grade" : "B", "score" : 22 }, { "date" : ISODate("2013-03-20T00:00:00Z"), "grade" : "B", "score" : 18 }, { "date" : ISO
"grade" : "B", "score" : 16 }, { "date" : ISODate("2011-12-14T00:00:00Z"), "grade" : "B", "score" : 17 } ], "name" : "Pho Bang Restaurant", "restaurant_id" : "41145163" }
{ "_id" : ObjectId("55c3043ab165fa6355ec756c"), "address" : { "building" : "342", "coord" : [ -73.986646, 40.7264594 ], "street" : "East 6 Street", "zipcode" : "10003" },
sine" : "Vietnamese/Cambodian/Malaysia", "grades" : [ { "date" : ISODate("2014-12-04T00:00:00Z"), "grade" : "A", "score" : 11 }, { "date" : ISODate("2014-06-27T00:00:00Z"
}, { "date" : ISODate("2013-07-30T00:00:00Z"), "grade" : "A", "score" : 13 }, { "date" : ISODate("2012-05-31T00:00:00Z"), "grade" : "A", "score" : 10 } ], "name" : "Lan C
550" }
{ "_id" : ObjectId("55c3043ab165fa6355ec761e"), "address" : { "building" : "18", "coord" : [ -74.0034562, 40.7275645 ], "street" : "King Street", "zipcode" : "10014" }, "
ne" : "Vietnamese/Cambodian/Malaysia", "grades" : [ { "date" : ISODate("2014-02-18T00:00:00Z"), "grade" : "A", "score" : 12 }, { "date" : ISODate("2013-08-20T00:00:00Z"),
, { "date" : ISODate("2013-03-05T00:00:00Z"), "grade" : "C", "score" : 45 }, { "date" : ISODate("2012-04-06T00:00:00Z"), "grade" : "B", "score" : 19 }, { "date" : ISODate
de" : "A", "score" : 9 } ], "name" : "New Mekong Restaurant", "restaurant_id" : "41202673" }
{ "_id" : ObjectId("55c3043ab165fa6355ec76e5"), "address" : { "building" : "4010", "coord" : [ -73.8915947, 40.746408 ], "street" : "74 Street", "zipcode" : "11373" }, "b
: "Vietnamese/Cambodian/Malaysia", "grades" : [ { "date" : ISODate("2014-08-12T00:00:00Z"), "grade" : "A", "score" : 7 }, { "date" : ISODate("2014-01-09T00:00:00Z"), "gra
date" : ISODate("2013-06-25T00:00:00Z"), "grade" : "A", "score" : 13 }, { "date" : ISODate("2012-05-07T00:00:00Z"), "grade" : "A", "score" : 12 }, { "date" : ISODate("201
 "A", "score" : 12 } ], "name" : "Thai Son", "restaurant_id" : "41221266" }
{ "_id" : ObjectId("55c3043ab165fa6355ec78f2"), "address" : { "building" : "369", "coord" : [ -73.9959432, 40.7200254 ], "street" : "Broome Street", "zipcode" : "10013" }
isine" : "Vietnamese/Cambodian/Malaysia", "grades" : [ { "date" : ISODate("2014-08-01T00:00:00Z"), "grade" : "A", "score" : 9 }, { "date" : ISODate("2014-02-11T00:00:00Z"
 }, { "date" : ISODate("2013-01-24T00:00:00Z"), "grade" : "A", "score" : 12 }, { "date" : ISODate("2012-06-22T00:00:00Z"), "grade" : "A", "score" : 5 }, { "date" : ISODat
ade" : "A", "score" : 2 } ], "name" : "Saigon Vietnamese Sandwich", "restaurant_id" : "41254832" }
{ "_id" : ObjectId("55c3043ab165fa6355ec7dfe"), "address" : { "building" : "82-90", "coord" : [ -73.8848687, 40.743896 ], "street" : "Broadway", "zipcode" : "11373" }, "b
: "Vietnamese/Cambodian/Malaysia", "grades" : [ { "date" : ISODate("2014-07-02T00:00:00Z"), "grade" : "B", "score" : 26 }, { "date" : ISODate("2013-10-23T00:00:00Z"), "gr
"date" : ISODate("2013-05-30T00:00:00Z"), "grade" : "B", "score" : 22 }, { "date" : ISODate("2012-12-18T00:00:00Z"), "grade" : "A", "score" : 7 }, { "date" : ISODate("201
 "C", "score" : 10 }, { "date" : ISODate("2012-02-24T00:00:00Z"), "grade" : "B", "score" : 17 } ], "name" : "Pho Bang Restaurant", "restaurant_id" : "41338955" }
{ "_id" : ObjectId("55c3043bb165fa6355ec8026"), "address" : { "building" : "102", "coord" : [ -73.99543899999999, 40.717846 ], "street" : "Bowery", "zipcode" : "10013" },
sine" : "Vietnamese/Cambodian/Malaysia", "grades" : [ { "date" : ISODate("2014-12-20T00:00:00Z"), "grade" : "A", "score" : 13 }, { "date" : ISODate("2014-07-08T00:00:00Z"
 }, { "date" : ISODate("2013-11-25T00:00:00Z"), "grade" : "A", "score" : 6 }, { "date" : ISODate("2013-04-04T00:00:00Z"), "grade" : "B", "score" : 15 }, { "date" : ISODat
ade" : "A", "score" : 13 }, { "date" : ISODate("2012-03-22T00:00:00Z"), "grade" : "A", "score" : 13 } ], "name" : "New Tu Do Restaurant", "restaurant_id" : "41374711" }
{ "_id" : ObjectId("55c3043bb165fa6355ec802f"), "address" : { "building" : "277", "coord" : [ -73.9927639, 40.717627 ], "street" : "Grand Street", "zipcode" : "10002" },
ine" : "Vietnamese/Cambodian/Malaysia", "grades" : [ { "date" : ISODate("2014-04-09T00:00:00Z"), "grade" : "A", "score" : 12 }, { "date" : ISODate("2013-09-09T00:00:00Z")
}, { "date" : ISODate("2013-04-24T00:00:00Z"), "grade" : "C", "score" : 0 }, { "date" : ISODate("2012-06-22T00:00:00Z"), "grade" : "A", "score" : 12 }, { "date" : ISODate
de" : "A", "score" : 11 }, { "date" : ISODate("2011-04-20T00:00:00Z"), "grade" : "B", "score" : 15 } ], "name" : "Pho Grand", "restaurant_id" : "41375457" }
{ "_id" : ObjectId("55c3043bb165fa6355ec8089"), "address" : { "building" : "3802", "coord" : [ -73.83283279999999, 40.7600184 ], "street" : "Prince Street", "zipcode" : "
, "cuisine" : "Vietnamese/Cambodian/Malaysia", "grades" : [ { "date" : ISODate("2014-06-02T00:00:00Z"), "grade" : "A", "score" : 9 }, { "date" : ISODate("2013-12-03T00:00
 : 11 }, { "date" : ISODate("2012-07-19T00:00:00Z"), "grade" : "A", "score" : 7 }, { "date" : ISODate("2012-03-06T00:00:00Z"), "grade" : "A", "score" : 9 } ], "name" : "P
staurant_id" : "41379217" }
{ "_id" : ObjectId("55c3043bb165fa6355ec8126"), "address" : { "building" : "85", "coord" : [ -73.9904772, 40.7179481 ], "street" : "Orchard Street", "zipcode" : "10002" }
isine" : "Vietnamese/Cambodian/Malaysia", "grades" : [ { "date" : ISODate("2014-12-18T00:00:00Z"), "grade" : "A", "score" : 5 }, { "date" : ISODate("2013-12-03T00:00:00Z"
}, { "date" : ISODate("2013-06-19T00:00:00Z"), "grade" : "A", "score" : 9 }, { "date" : ISODate("2012-11-21T00:00:00Z"), "grade" : "B", "score" : 20 }, { "date" : ISODate
de" : "A", "score" : 9 } ], "name" : "An Choi", "restaurant_id" : "41386399" }
{ "_id" : ObjectId("55c3043bb165fa6355ec8398"), "address" : { "building" : "120", "coord" : [ -74.005578, 40.732983 ], "street" : "Christopher Street", "zipcode" : "10014
"cuisine" : "Vietnamese/Cambodian/Malaysia", "grades" : [ { "date" : ISODate("2014-09-16T00:00:00Z"), "grade" : "A", "score" : 13 }, { "date" : ISODate("2014-04-28T00:00:
: 2 }, { "date" : ISODate("2013-02-28T00:00:00Z"), "grade" : "A", "score" : 10 }, { "date" : ISODate("2012-10-19T00:00:00Z"), "grade" : "C", "score" : 5 }, { "date" : ISO
"grade" : "B", "score" : 20 }, { "date" : ISODate("2011-11-14T00:00:00Z"), "grade" : "A", "score" : 7 } ], "name" : "Baoguette Pho Sure", "restaurant_id" : "41414673" }
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.

Previous: Write a MongoDB query to find the restaurants which do not prepare any cuisine of American and achieved a score more than 70 and located in the longitude less than -65.754168.
Next: Write a MongoDB query to find the restaurant Id, name, borough and cuisine for those restaurants which contain 'Wil' as first three letters for its name.

What is the difficulty level of this exercise?



New Exercises:R Programming