w3resource

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("564c2d949eb21ad392f1ba3c"), "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" : ISODate("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("564c2d949eb21ad392f1c0d1"), "address" : { "building" : "169", "coord" : [ -74.000466, 40.727688 ], "street" : "Thompson Street", "zipcode" : "10012" }, "borough" : "Manhattan", "cuisine" : "Other", "grades" : [ { "date" : ISODate("2014-05-15T00:00:00Z"), "grade" : "A", "score" : 10 } ], "name" : "Zz Clam Bar", "restaurant_id" : "50001062" }
{ "_id" : ObjectId("564c2d949eb21ad392f1bc98"), "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("2013-08-22T00:00:00Z"), "grade" : "B", "score" : 18 } ], "name" : "Zymi Bar & Grill", "restaurant_id"
 : "41712791" }
{ "_id" : ObjectId("564c2d949eb21ad392f19aa9"), "address" : { "building" : "173", "coord" : [ -73.98195249999999, 40.6784779 ], "street" : "4 Avenue", "zipcode" : "11217" }, "borough" : "Brooklyn", "cuisine" : "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" : ISODate("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("564c2d949eb21ad392f1ac9c"), "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", "score" : 40 } ], "name" : "Zutto", "restaurant_id" : "41602974" }
{ "_id" : ObjectId("564c2d949eb21ad392f1b956"), "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("564c2d949eb21ad392f1d5a2"), "address" : { "building" : "261", "coord" : [ -73.980608, 40.750468 ], "street" : "Madison Ave", "zipcode" : "10016" }, "borough" : "Manhattan", "cuisine" : "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("564c2d939eb21ad392f17688"), "address" : { "building" : "6946", "coord" : [ -73.8811834, 40.7017759 ], "street" : "Myrtle Avenue", "zipcode" : "11385" }, "borough" : "Queens", "cuisine" : "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("2013-03-12T00:00:00Z"), "grade" : "A", "score" : 13 }, { "date" : ISODate("2012-10-02T00:00:00Z"), "g
rade" : "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("564c2d949eb21ad392f181c3"), "address" : { "building" : "107109", "coord" : [ -73.9744668, 40.731155 ], "street" : "Avenue C", "zipcode" : "10009" }, "borough" : "Manhattan", "cuisine" : "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("2013-02-27T00:00:00Z"), "grade" : "A", "score" : 11 }, { "date" : ISODate("2012-06-05T00:00:00Z"), "gr
ade" : "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("564c2d949eb21ad392f1941a"), "address" : { "building" : "146", "coord" : [ -74.00985849999999, 40.7155442 ], "street" : "Chambers Street", "zipcode" : "10007" }, "borough" : "Manhattan", "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("564c2d949eb21ad392f1baf9"), "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", "restau
rant_id" : "41705626" }
{ "_id" : ObjectId("564c2d949eb21ad392f1ab5f"), "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("564c2d949eb21ad392f18ede"), "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"), "grad
e" : "B", "score" : 16 } ], "name" : "Zucchero E Pomodori", "restaurant_id" : "41189590" }
{ "_id" : ObjectId("564c2d949eb21ad392f1b7c1"), "address" : { "building" : "18816", "coord" : [ -73.7804838, 40.7291221 ], "street" : "Union Turnpike", "zipcode" : "11366" }, "borough" : "Queens", "cuisine" : "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("564c2d949eb21ad392f1c66d"), "address" : { "building" : "4114", "coord" : [ -73.80402889999999, 40.76095919999999 ], "street" : "162Nd St", "zipcode" : "11358" }, "borough" : "Queens", "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("564c2d949eb21ad392f183f9"), "address" : { "building" : "2905", "coord" : [ -73.91337299999999, 40.775064 ], "street" : "23 Avenue", "zipcode" : "11105" }, "borough" : "Queens", "cuisine" : "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" : 12 } ], "name" : "Zorba'S", "restaurant_id" : "40877247" }
{ "_id" : ObjectId("564c2d949eb21ad392f1bf47"), "address" : { "building" : "449", "coord" : [ -73.990179, 40.765078 ], "street" : "West   51 Street", "zipcode" : "10019" }, "borough" : "Manhattan", "cuisine" : "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("564c2d949eb21ad392f19f1a"), "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("2012-01-05T00:00:00Z"), "grade" : "A", "score" : 2 } ], "name" : "Zoodo", "restaurant_id" : "41446358"
}
{ "_id" : ObjectId("564c2d949eb21ad392f1b128"), "address" : { "building" : "460462", "coord" : [ -73.9935881, 40.7571543 ], "street" : "9Th Ave", "zipcode" : "10018" }, "borough" : "Manhattan", "cuisine" : "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", "restaurant_id" : "41636858" }
{ "_id" : ObjectId("564c2d949eb21ad392f1d66b"), "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

Explanation:

The said query in MongoDB query that searches for all documents in the restaurants collection and sorts the result set in descending order based on the name field.
The sort() method is then called on the result set to sort the documents based on the name field in descending order (-1 specifies descending order, while 1 would specify ascending order).

Improve this sample solution and post your code through Disqus.

Previous: Arrange the name of the restaurants in ascending order along with all the columns.
Next: Rank cuisines ascendingly, and boroughs descendingly for the same cuisine.

What is the difficulty level of this exercise?



Become a Patron!

Follow us on Facebook and Twitter for latest update.

It will be nice if you may share this link in any developer community or anywhere else, from where other developers may find this content. Thanks.

https://w3resource.com/mongodb-exercises/mongodb-exercise-26.php