w3resource
w3resource logo

MongoDB Exercise - Display the name of the restaurants in descending along with all the columns


Write a MongoDB query to arrange the name of the restaurants in descending along with all the columns.

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().sort(
                          {"name":-1}
                          );

Output:

{ "_id" : ObjectId("5ab23ff163b071e808060947"), "address" : { "building" : "1", "coord" : [ -74.073156, 40.6457369 ], "street" : "Richmond Terrace", "zipcode" : "10301" }, "borough" : "Staten Island",
 "cuisine" : "Pizza", "grades" : [ { "date" : ISODate("2015-01-13T00:00:00Z"), "grade" : "Z", "score" : 18 }, { "date" : ISODate("2014-07-24T00:00:00Z"), "grade" : "A", "score" : 12 }, { "date" : ISOD
ate("2013-11-08T00:00:00Z"), "grade" : "B", "score" : 21 }, { "date" : ISODate("2013-04-17T00:00:00Z"), "grade" : "A", "score" : 12 } ], "name" : "Zz'S Pizza & Grill", "restaurant_id" : "41702705" }
{ "_id" : ObjectId("5ab23ff163b071e808060fde"), "address" : { "building" : "169", "coord" : [ -74.000466, 40.727688 ], "street" : "Thompson Street", "zipcode" : "10012" }, "borough" : "Manhattan", "cu
isine" : "Other", "grades" : [ { "date" : ISODate("2014-05-15T00:00:00Z"), "grade" : "A", "score" : 10 } ], "name" : "Zz Clam Bar", "restaurant_id" : "50001062" }
{ "_id" : ObjectId("5ab23ff163b071e808060ba5"), "address" : { "building" : "726", "coord" : [ -73.8662887, 40.8594314 ], "street" : "Astor Avenue", "zipcode" : "10467" }, "borough" : "Bronx", "cuisine
" : "American", "grades" : [ { "date" : ISODate("2014-09-09T00:00:00Z"), "grade" : "C", "score" : 75 }, { "date" : ISODate("2014-03-26T00:00:00Z"), "grade" : "A", "score" : 5 }, { "date" : ISODate("20
13-08-22T00:00:00Z"), "grade" : "B", "score" : 18 } ], "name" : "Zymi Bar & Grill", "restaurant_id" : "41712791" }
{ "_id" : ObjectId("5ab23ff163b071e80805e999"), "address" : { "building" : "173", "coord" : [ -73.98195249999999, 40.6784779 ], "street" : "4 Avenue", "zipcode" : "11217" }, "borough" : "Brooklyn", "c
uisine" : "Japanese", "grades" : [ { "date" : ISODate("2014-07-01T00:00:00Z"), "grade" : "A", "score" : 11 }, { "date" : ISODate("2013-12-20T00:00:00Z"), "grade" : "A", "score" : 9 }, { "date" : ISODa
te("2013-03-21T00:00:00Z"), "grade" : "A", "score" : 7 }, { "date" : ISODate("2011-12-22T00:00:00Z"), "grade" : "A", "score" : 4 } ], "name" : "Zuzu Ramen", "restaurant_id" : "41387911" }
{ "_id" : ObjectId("5ab23ff163b071e80805fba5"), "address" : { "building" : "77", "coord" : [ -74.0090803, 40.71848110000001 ], "street" : "Hudson Street", "zipcode" : "10013" }, "borough" : "Manhattan
", "cuisine" : "Japanese", "grades" : [ { "date" : ISODate("2014-11-12T00:00:00Z"), "grade" : "B", "score" : 14 }, { "date" : ISODate("2014-04-07T00:00:00Z"), "grade" : "B", "score" : 17 }, { "date" :
 ISODate("2013-07-29T00:00:00Z"), "grade" : "A", "score" : 5 }, { "date" : ISODate("2013-01-31T00:00:00Z"), "grade" : "A", "score" : 12 }, { "date" : ISODate("2012-05-03T00:00:00Z"), "grade" : "C", "s
core" : 40 } ], "name" : "Zutto", "restaurant_id" : "41602974" }
{ "_id" : ObjectId("5ab23ff163b071e808060862"), "address" : { "building" : "330", "coord" : [ -74.134152, 40.632998 ], "street" : "Heberton Avenue", "zipcode" : "10302" }, "borough" : "Staten Island",
 "cuisine" : "Other", "grades" : [ { "date" : ISODate("2015-01-20T00:00:00Z"), "grade" : "Not Yet Graded", "score" : 44 } ], "name" : "Zumba Restaurant", "restaurant_id" : "41698293" }
{ "_id" : ObjectId("5ab23ff163b071e8080624b6"), "address" : { "building" : "261", "coord" : [ -73.980608, 40.750468 ], "street" : "Madison Ave", "zipcode" : "10016" }, "borough" : "Manhattan", "cuisin
e" : "Japanese", "grades" : [ { "date" : ISODate("2015-01-20T00:00:00Z"), "grade" : "Not Yet Graded", "score" : 17 } ], "name" : "Zuma Japanese Restaurant New York", "restaurant_id" : "50017098" }
{ "_id" : ObjectId("5ab23ff163b071e80805c584"), "address" : { "building" : "6946", "coord" : [ -73.8811834, 40.7017759 ], "street" : "Myrtle Avenue", "zipcode" : "11385" }, "borough" : "Queens", "cuis
ine" : "German", "grades" : [ { "date" : ISODate("2014-09-24T00:00:00Z"), "grade" : "A", "score" : 11 }, { "date" : ISODate("2014-04-17T00:00:00Z"), "grade" : "A", "score" : 7 }, { "date" : ISODate("2
013-03-12T00:00:00Z"), "grade" : "A", "score" : 13 }, { "date" : ISODate("2012-10-02T00:00:00Z"), "grade" : "A", "score" : 9 }, { "date" : ISODate("2012-05-09T00:00:00Z"), "grade" : "A", "score" : 13
}, { "date" : ISODate("2011-12-28T00:00:00Z"), "grade" : "B", "score" : 24 } ], "name" : "Zum Stammtisch", "restaurant_id" : "40367377" }
{ "_id" : ObjectId("5ab23ff163b071e80805d0c0"), "address" : { "building" : "107109", "coord" : [ -73.9744668, 40.731155 ], "street" : "Avenue C", "zipcode" : "10009" }, "borough" : "Manhattan", "cuisi
ne" : "German", "grades" : [ { "date" : ISODate("2014-03-04T00:00:00Z"), "grade" : "A", "score" : 7 }, { "date" : ISODate("2013-08-21T00:00:00Z"), "grade" : "A", "score" : 13 }, { "date" : ISODate("20
13-02-27T00:00:00Z"), "grade" : "A", "score" : 11 }, { "date" : ISODate("2012-06-05T00:00:00Z"), "grade" : "A", "score" : 12 }, { "date" : ISODate("2011-12-20T00:00:00Z"), "grade" : "A", "score" : 9 }
, { "date" : ISODate("2011-07-15T00:00:00Z"), "grade" : "A", "score" : 6 } ], "name" : "Zum Schneider", "restaurant_id" : "40763382" }
{ "_id" : ObjectId("5ab23ff163b071e80805e31c"), "address" : { "building" : "146", "coord" : [ -74.00985849999999, 40.7155442 ], "street" : "Chambers Street", "zipcode" : "10007" }, "borough" : "Manhat
tan", "cuisine" : "Bagels/Pretzels", "grades" : [ { "date" : ISODate("2014-04-02T00:00:00Z"), "grade" : "A", "score" : 10 }, { "date" : ISODate("2013-10-31T00:00:00Z"), "grade" : "A", "score" : 5 }, {
 "date" : ISODate("2013-06-07T00:00:00Z"), "grade" : "A", "score" : 4 }, { "date" : ISODate("2012-12-28T00:00:00Z"), "grade" : "A", "score" : 13 }, { "date" : ISODate("2011-12-20T00:00:00Z"), "grade"
: "A", "score" : 12 } ], "name" : "Zucker'S Bagels And Smoked Fish", "restaurant_id" : "41290213" }
{ "_id" : ObjectId("5ab23ff163b071e808060a05"), "address" : { "building" : "370", "coord" : [ -73.9768121, 40.7507385 ], "street" : "Lexington Avenue", "zipcode" : "10017" }, "borough" : "Manhattan",
"cuisine" : "Delicatessen", "grades" : [ { "date" : ISODate("2015-01-15T00:00:00Z"), "grade" : "A", "score" : 9 }, { "date" : ISODate("2014-07-01T00:00:00Z"), "grade" : "A", "score" : 13 }, { "date" :
 ISODate("2013-06-12T00:00:00Z"), "grade" : "A", "score" : 7 } ], "name" : "Zucker'S Bagel", "restaurant_id" : "41705626" }
{ "_id" : ObjectId("5ab23ff163b071e80805fa67"), "address" : { "building" : "433", "coord" : [ -73.9833124, 40.7277147 ], "street" : "East    9 Street", "zipcode" : "10009" }, "borough" : "Manhattan",
"cuisine" : "Café/Coffee/Tea", "grades" : [ { "date" : ISODate("2014-05-14T00:00:00Z"), "grade" : "A", "score" : 10 }, { "date" : ISODate("2013-05-14T00:00:00Z"), "grade" : "A", "score" : 7 }, { "date
" : ISODate("2012-12-19T00:00:00Z"), "grade" : "A", "score" : 2 }, { "date" : ISODate("2011-09-23T00:00:00Z"), "grade" : "A", "score" : 0 } ], "name" : "Zucker Bakery", "restaurant_id" : "41590236" }
{ "_id" : ObjectId("5ab23ff163b071e80805dde0"), "address" : { "building" : "1435", "coord" : [ -73.95734, 40.770682 ], "street" : "2 Avenue", "zipcode" : "10021" }, "borough" : "Manhattan", "cuisine"
: "Italian", "grades" : [ { "date" : ISODate("2014-05-01T00:00:00Z"), "grade" : "A", "score" : 13 }, { "date" : ISODate("2013-03-07T00:00:00Z"), "grade" : "A", "score" : 12 }, { "date" : ISODate("2012
-07-31T00:00:00Z"), "grade" : "B", "score" : 24 }, { "date" : ISODate("2012-03-08T00:00:00Z"), "grade" : "B", "score" : 16 } ], "name" : "Zucchero E Pomodori", "restaurant_id" : "41189590" }
{ "_id" : ObjectId("5ab23ff163b071e8080606cc"), "address" : { "building" : "18816", "coord" : [ -73.7804838, 40.7291221 ], "street" : "Union Turnpike", "zipcode" : "11366" }, "borough" : "Queens", "cu
isine" : "Ice Cream, Gelato, Yogurt, Ices", "grades" : [ { "date" : ISODate("2013-12-28T00:00:00Z"), "grade" : "A", "score" : 4 }, { "date" : ISODate("2012-12-13T00:00:00Z"), "grade" : "A", "score" :
7 } ], "name" : "Zucca Buca Frozen Yogurt", "restaurant_id" : "41688644" }
{ "_id" : ObjectId("5ab23ff163b071e80806157c"), "address" : { "building" : "4114", "coord" : [ -73.80402889999999, 40.76095919999999 ], "street" : "162Nd St", "zipcode" : "11358" }, "borough" : "Queen
s", "cuisine" : "Korean", "grades" : [ { "date" : ISODate("2014-12-10T00:00:00Z"), "grade" : "Z", "score" : 12 }, { "date" : ISODate("2014-05-09T00:00:00Z"), "grade" : "B", "score" : 20 } ], "name" :
"Zu Rang Ny", "restaurant_id" : "50004270" }
{ "_id" : ObjectId("5ab23ff163b071e80805d2fa"), "address" : { "building" : "2905", "coord" : [ -73.91337299999999, 40.775064 ], "street" : "23 Avenue", "zipcode" : "11105" }, "borough" : "Queens", "cu
isine" : "Greek", "grades" : [ { "date" : ISODate("2014-09-29T00:00:00Z"), "grade" : "A", "score" : 7 }, { "date" : ISODate("2014-04-18T00:00:00Z"), "grade" : "A", "score" : 13 }, { "date" : ISODate("
2013-03-19T00:00:00Z"), "grade" : "A", "score" : 12 }, { "date" : ISODate("2012-10-03T00:00:00Z"), "grade" : "B", "score" : 20 }, { "date" : ISODate("2012-05-07T00:00:00Z"), "grade" : "A", "score" : 1
2 } ], "name" : "Zorba'S", "restaurant_id" : "40877247" }
{ "_id" : ObjectId("5ab23ff163b071e808060e54"), "address" : { "building" : "449", "coord" : [ -73.990179, 40.765078 ], "street" : "West   51 Street", "zipcode" : "10019" }, "borough" : "Manhattan", "c
uisine" : "Latin (Cuban, Dominican, Puerto Rican, South & Central American)", "grades" : [ { "date" : ISODate("2014-10-15T00:00:00Z"), "grade" : "A", "score" : 13 }, { "date" : ISODate("2014-04-28T00:
00:00Z"), "grade" : "A", "score" : 8 }, { "date" : ISODate("2013-10-18T00:00:00Z"), "grade" : "A", "score" : 9 } ], "name" : "Zoralie Restaurant Inc.", "restaurant_id" : "50000338" }
{ "_id" : ObjectId("5ab23ff163b071e80805ee1f"), "address" : { "building" : "1518", "coord" : [ -73.917182, 40.843466 ], "street" : "Macombs Road", "zipcode" : "10452" }, "borough" : "Bronx", "cuisine"
 : "African", "grades" : [ { "date" : ISODate("2013-09-05T00:00:00Z"), "grade" : "Z", "score" : 11 }, { "date" : ISODate("2013-01-25T00:00:00Z"), "grade" : "A", "score" : 12 }, { "date" : ISODate("201
2-01-05T00:00:00Z"), "grade" : "A", "score" : 2 } ], "name" : "Zoodo", "restaurant_id" : "41446358" }
{ "_id" : ObjectId("5ab23ff163b071e808060031"), "address" : { "building" : "460462", "coord" : [ -73.9935881, 40.7571543 ], "street" : "9Th Ave", "zipcode" : "10018" }, "borough" : "Manhattan", "cuisi
ne" : "Thai", "grades" : [ { "date" : ISODate("2014-11-19T00:00:00Z"), "grade" : "A", "score" : 0 }, { "date" : ISODate("2014-07-17T00:00:00Z"), "grade" : "A", "score" : 9 }, { "date" : ISODate("2014-
06-23T00:00:00Z"), "grade" : "P", "score" : 5 }, { "date" : ISODate("2014-02-12T00:00:00Z"), "grade" : "C", "score" : 32 }, { "date" : ISODate("2013-07-24T00:00:00Z"), "grade" : "B", "score" : 21 }, {
 "date" : ISODate("2013-01-23T00:00:00Z"), "grade" : "A", "score" : 13 }, { "date" : ISODate("2012-04-19T00:00:00Z"), "grade" : "B", "score" : 20 } ], "name" : "Zoob Zib Thai Noodle / Aura", "restaura
nt_id" : "41636858" }
{ "_id" : ObjectId("5ab23ff163b071e80806257e"), "address" : { "building" : "22", "coord" : [ -73.98645739999999, 40.7489646 ], "street" : "W 34Th St", "zipcode" : "10001" }, "borough" : "Manhattan", "
cuisine" : "Café/Coffee/Tea", "grades" : [ { "date" : ISODate("2014-12-05T00:00:00Z"), "grade" : "A", "score" : 12 } ], "name" : "Zoni Cafe", "restaurant_id" : "50017605" }
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.