w3resource
w3resource logo

MongoDB Exercise - Display the name of the cuisine in ascending order and for that same cuisine borough should be in descending order


Write a MongoDB query to arranged the name of the cuisine in ascending order and for that same cuisine borough should be 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().sort(
                           {"cuisine":1,"borough" : -1,}
                          );

Output:

{ "_id" : ObjectId("5ab23ff163b071e80805f529"), "address" : { "building" : "259-11", "coord" : [ -73.708831, 40.73748399999999 ], "street" : "Hillside Avenue", "zipcode" : "11004" }, "borough" : "Quee
ns", "cuisine" : "Afghan", "grades" : [ { "date" : ISODate("2014-09-15T00:00:00Z"), "grade" : "A", "score" : 13 }, { "date" : ISODate("2013-09-18T00:00:00Z"), "grade" : "A", "score" : 7 }, { "date" :
ISODate("2013-04-18T00:00:00Z"), "grade" : "B", "score" : 21 }, { "date" : ISODate("2012-08-29T00:00:00Z"), "grade" : "A", "score" : 2 }, { "date" : ISODate("2012-02-09T00:00:00Z"), "grade" : "A", "sc
ore" : 11 } ], "name" : "Afghan Kebob House", "restaurant_id" : "41535706" }
{ "_id" : ObjectId("5ab23ff163b071e80805f75a"), "address" : { "building" : "68-16", "coord" : [ -73.79474420000001, 40.7332144 ], "street" : "Fresh Meadow Lane", "zipcode" : "11365" }, "borough" : "Qu
eens", "cuisine" : "Afghan", "grades" : [ { "date" : ISODate("2013-11-23T00:00:00Z"), "grade" : "A", "score" : 13 }, { "date" : ISODate("2013-06-01T00:00:00Z"), "grade" : "A", "score" : 13 }, { "date"
 : ISODate("2012-02-27T00:00:00Z"), "grade" : "A", "score" : 13 }, { "date" : ISODate("2011-09-03T00:00:00Z"), "grade" : "C", "score" : 34 } ], "name" : "Bakhter Afghan Halal Kabab", "restaurant_id" :
 "41559771" }
{ "_id" : ObjectId("5ab23ff163b071e80805f856"), "address" : { "building" : "43-27", "coord" : [ -73.8260645, 40.751886 ], "street" : "Main Street", "zipcode" : "11355" }, "borough" : "Queens", "cuisin
e" : "Afghan", "grades" : [ { "date" : ISODate("2014-02-25T00:00:00Z"), "grade" : "A", "score" : 12 }, { "date" : ISODate("2013-08-29T00:00:00Z"), "grade" : "B", "score" : 15 }, { "date" : ISODate("20
13-03-05T00:00:00Z"), "grade" : "A", "score" : 13 }, { "date" : ISODate("2013-02-13T00:00:00Z"), "grade" : "P", "score" : 5 }, { "date" : ISODate("2012-06-28T00:00:00Z"), "grade" : "B", "score" : 17 }
, { "date" : ISODate("2011-06-18T00:00:00Z"), "grade" : "A", "score" : 13 } ], "name" : "Choopan Kabab Restaurant", "restaurant_id" : "41569155" }
{ "_id" : ObjectId("5ab23ff163b071e808060399"), "address" : { "building" : "6729", "coord" : [ -73.8204147, 40.724243 ], "street" : "Main St", "zipcode" : "11367" }, "borough" : "Queens", "cuisine" :
"Afghan", "grades" : [ { "date" : ISODate("2014-08-20T00:00:00Z"), "grade" : "A", "score" : 4 }, { "date" : ISODate("2013-07-31T00:00:00Z"), "grade" : "A", "score" : 5 }, { "date" : ISODate("2012-08-0
4T00:00:00Z"), "grade" : "A", "score" : 5 } ], "name" : "Bakhtar Kabab", "restaurant_id" : "41661199" }
{ "_id" : ObjectId("5ab23ff163b071e8080611c8"), "address" : { "building" : "110-23", "coord" : [ -73.8374947, 40.696891 ], "street" : "Jamaica Avenue", "zipcode" : "11418" }, "borough" : "Queens", "cu
isine" : "Afghan", "grades" : [ { "date" : ISODate("2014-07-09T00:00:00Z"), "grade" : "A", "score" : 12 }, { "date" : ISODate("2014-01-02T00:00:00Z"), "grade" : "A", "score" : 11 } ], "name" : "New Bk
hatar Afghan Halal Kabab & Gyro King", "restaurant_id" : "50001906" }
{ "_id" : ObjectId("5ab23ff163b071e808061990"), "address" : { "building" : "2310", "coord" : [ -73.91336969999999, 40.7742674 ], "street" : "31St St", "zipcode" : "11105" }, "borough" : "Queens", "cui
sine" : "Afghan", "grades" : [ { "date" : ISODate("2014-03-27T00:00:00Z"), "grade" : "A", "score" : 13 } ], "name" : "Balkh Shish Kabab House", "restaurant_id" : "50007432" }
{ "_id" : ObjectId("5ab23ff163b071e808061ab0"), "address" : { "building" : "33-35", "coord" : [ -73.7896131, 40.7666136 ], "street" : "Francis Lewis Boulevard", "zipcode" : "11358" }, "borough" : "Que
ens", "cuisine" : "Afghan", "grades" : [ { "date" : ISODate("2014-06-25T00:00:00Z"), "grade" : "A", "score" : 11 } ], "name" : "Arya Kabob House", "restaurant_id" : "50008452" }
{ "_id" : ObjectId("5ab23ff163b071e808061d28"), "address" : { "building" : "72-55", "coord" : [ -73.812895, 40.7276946 ], "street" : "Kissena Boulevard", "zipcode" : "11367" }, "borough" : "Queens", "
cuisine" : "Afghan", "grades" : [ { "date" : ISODate("2014-08-28T00:00:00Z"), "grade" : "A", "score" : 12 } ], "name" : "Tariq Afghan Kabab", "restaurant_id" : "50010806" }
{ "_id" : ObjectId("5ab23ff163b071e808061d2b"), "address" : { "building" : "68-16", "coord" : [ -73.79474420000001, 40.7332144 ], "street" : "Fresh Meadow Lane", "zipcode" : "11365" }, "borough" : "Qu
eens", "cuisine" : "Afghan", "grades" : [ { "date" : ISODate("2015-01-12T00:00:00Z"), "grade" : "A", "score" : 10 }, { "date" : ISODate("2014-06-26T00:00:00Z"), "grade" : "A", "score" : 12 } ], "name"
 : "Bakhter Afghan Halal Kabab", "restaurant_id" : "50010813" }
{ "_id" : ObjectId("5ab23ff163b071e80805cbb0"), "address" : { "building" : "1345", "coord" : [ -73.959249, 40.768076 ], "street" : "2 Avenue", "zipcode" : "10021" }, "borough" : "Manhattan", "cuisine"
 : "Afghan", "grades" : [ { "date" : ISODate("2014-10-07T00:00:00Z"), "grade" : "A", "score" : 9 }, { "date" : ISODate("2013-10-23T00:00:00Z"), "grade" : "A", "score" : 8 }, { "date" : ISODate("2012-1
0-26T00:00:00Z"), "grade" : "A", "score" : 13 }, { "date" : ISODate("2012-04-26T00:00:00Z"), "grade" : "A", "score" : 7 }, { "date" : ISODate("2012-01-12T00:00:00Z"), "grade" : "P", "score" : 10 } ],
"name" : "Afghan Kebab House", "restaurant_id" : "40552806" }
{ "_id" : ObjectId("5ab23ff163b071e80805ccdd"), "address" : { "building" : "34", "coord" : [ -73.9883612, 40.7286391 ], "street" : "St Marks Place", "zipcode" : "10003" }, "borough" : "Manhattan", "cu
isine" : "Afghan", "grades" : [ { "date" : ISODate("2014-02-20T00:00:00Z"), "grade" : "A", "score" : 12 }, { "date" : ISODate("2013-08-06T00:00:00Z"), "grade" : "B", "score" : 17 }, { "date" : ISODate
("2013-01-31T00:00:00Z"), "grade" : "A", "score" : 13 }, { "date" : ISODate("2012-05-24T00:00:00Z"), "grade" : "A", "score" : 10 }, { "date" : ISODate("2011-12-13T00:00:00Z"), "grade" : "A", "score" :
 10 }, { "date" : ISODate("2011-08-25T00:00:00Z"), "grade" : "A", "score" : 10 } ], "name" : "Khyber Pass", "restaurant_id" : "40589545" }
{ "_id" : ObjectId("5ab23ff163b071e80805cd95"), "address" : { "building" : "764", "coord" : [ -73.9880435, 40.7641423 ], "street" : "9 Avenue", "zipcode" : "10019" }, "borough" : "Manhattan", "cuisine
" : "Afghan", "grades" : [ { "date" : ISODate("2014-12-30T00:00:00Z"), "grade" : "A", "score" : 12 }, { "date" : ISODate("2013-10-10T00:00:00Z"), "grade" : "B", "score" : 19 }, { "date" : ISODate("201
3-02-25T00:00:00Z"), "grade" : "A", "score" : 13 }, { "date" : ISODate("2012-08-09T00:00:00Z"), "grade" : "C", "score" : 2 }, { "date" : ISODate("2012-02-16T00:00:00Z"), "grade" : "A", "score" : 9 } ]
, "name" : "Afghan Kebab House #1", "restaurant_id" : "40616799" }
{ "_id" : ObjectId("5ab23ff163b071e80805d2b8"), "address" : { "building" : "787", "coord" : [ -73.9880886, 40.7651331 ], "street" : "9 Avenue", "zipcode" : "10019" }, "borough" : "Manhattan", "cuisine
" : "Afghan", "grades" : [ { "date" : ISODate("2014-07-22T00:00:00Z"), "grade" : "A", "score" : 3 }, { "date" : ISODate("2013-07-08T00:00:00Z"), "grade" : "A", "score" : 2 }, { "date" : ISODate("2013-
02-05T00:00:00Z"), "grade" : "A", "score" : 2 }, { "date" : ISODate("2011-11-28T00:00:00Z"), "grade" : "A", "score" : 13 } ], "name" : "Ariana Kebab House", "restaurant_id" : "40868400" }
{ "_id" : ObjectId("5ab23ff163b071e80806047e"), "address" : { "building" : "984", "coord" : [ -73.9673814, 40.63341399999999 ], "street" : "Coney Island Avenue", "zipcode" : "11230" }, "borough" : "Br
ooklyn", "cuisine" : "Afghan", "grades" : [ { "date" : ISODate("2014-09-19T00:00:00Z"), "grade" : "C", "score" : 38 }, { "date" : ISODate("2014-03-27T00:00:00Z"), "grade" : "A", "score" : 12 }, { "dat
e" : ISODate("2014-02-12T00:00:00Z"), "grade" : "P", "score" : 6 }, { "date" : ISODate("2013-06-18T00:00:00Z"), "grade" : "A", "score" : 10 }, { "date" : ISODate("2012-06-05T00:00:00Z"), "grade" : "A"
, "score" : 6 } ], "name" : "Bahar Masala", "restaurant_id" : "41670224" }
{ "_id" : ObjectId("5ab23ff163b071e8080603a2"), "address" : { "building" : "358", "coord" : [ -74.0850586, 40.6183936 ], "street" : "Targee Street", "zipcode" : "10304" }, "borough" : "Staten Island",
 "cuisine" : "African", "grades" : [ { "date" : ISODate("2014-06-18T00:00:00Z"), "grade" : "A", "score" : 9 }, { "date" : ISODate("2013-10-02T00:00:00Z"), "grade" : "A", "score" : 10 }, { "date" : ISO
Date("2012-09-13T00:00:00Z"), "grade" : "A", "score" : 13 } ], "name" : "Mona'S Cuisine", "restaurant_id" : "41661732" }
{ "_id" : ObjectId("5ab23ff163b071e808061182"), "address" : { "building" : "611", "coord" : [ -74.07564099999999, 40.627075 ], "street" : "Bay Street", "zipcode" : "10304" }, "borough" : "Staten Islan
d", "cuisine" : "African", "grades" : [ { "date" : ISODate("2014-11-19T00:00:00Z"), "grade" : "A", "score" : 9 }, { "date" : ISODate("2013-11-07T00:00:00Z"), "grade" : "A", "score" : 13 } ], "name" :
"Wazobia Restaurant", "restaurant_id" : "50001806" }
{ "_id" : ObjectId("5ab23ff163b071e80805d12e"), "address" : { "building" : "14612", "coord" : [ -73.8057078, 40.6948408 ], "street" : "Liberty Avenue", "zipcode" : "11435" }, "borough" : "Queens", "cu
isine" : "African", "grades" : [ { "date" : ISODate("2014-04-16T00:00:00Z"), "grade" : "A", "score" : 4 }, { "date" : ISODate("2013-09-16T00:00:00Z"), "grade" : "A", "score" : 2 }, { "date" : ISODate(
"2012-08-23T00:00:00Z"), "grade" : "A", "score" : 13 }, { "date" : ISODate("2012-02-21T00:00:00Z"), "grade" : "A", "score" : 11 }, { "date" : ISODate("2011-06-30T00:00:00Z"), "grade" : "B", "score" :
19 } ], "name" : "Africana Restaurant", "restaurant_id" : "40798504" }
{ "_id" : ObjectId("5ab23ff163b071e80805e17a"), "address" : { "building" : "138-24", "coord" : [ -73.76439599999999, 40.6721779 ], "street" : "Farmers Boulevard", "zipcode" : "11434" }, "borough" : "Q
ueens", "cuisine" : "African", "grades" : [ { "date" : ISODate("2014-02-20T00:00:00Z"), "grade" : "A", "score" : 9 }, { "date" : ISODate("2013-08-13T00:00:00Z"), "grade" : "A", "score" : 10 }, { "date
" : ISODate("2013-03-19T00:00:00Z"), "grade" : "A", "score" : 3 }, { "date" : ISODate("2012-01-19T00:00:00Z"), "grade" : "A", "score" : 10 } ], "name" : "Aso Rock Lounge And Restaurant", "restaurant_i
d" : "41260089" }
{ "_id" : ObjectId("5ab23ff163b071e80805f273"), "address" : { "building" : "14407", "coord" : [ -73.8112644, 40.7023682 ], "street" : "Jamaica Avenue", "zipcode" : "11435" }, "borough" : "Queens", "cu
isine" : "African", "grades" : [ { "date" : ISODate("2014-07-19T00:00:00Z"), "grade" : "A", "score" : 10 }, { "date" : ISODate("2014-01-02T00:00:00Z"), "grade" : "A", "score" : 13 }, { "date" : ISODat
e("2013-07-09T00:00:00Z"), "grade" : "A", "score" : 10 }, { "date" : ISODate("2012-12-03T00:00:00Z"), "grade" : "A", "score" : 11 }, { "date" : ISODate("2012-05-31T00:00:00Z"), "grade" : "A", "score"
: 11 } ], "name" : "Mataheko African Restaurant And Lounge", "restaurant_id" : "41495376" }
{ "_id" : ObjectId("5ab23ff163b071e808060932"), "address" : { "building" : "153-41", "coord" : [ -73.78218799999999, 40.669525 ], "street" : "Rockaway Boulevard", "zipcode" : "11434" }, "borough" : "Q
ueens", "cuisine" : "African", "grades" : [ { "date" : ISODate("2014-08-21T00:00:00Z"), "grade" : "A", "score" : 11 }, { "date" : ISODate("2014-04-03T00:00:00Z"), "grade" : "B", "score" : 26 }, { "dat
e" : ISODate("2013-03-05T00:00:00Z"), "grade" : "A", "score" : 11 } ], "name" : "Tropical Grill", "restaurant_id" : "41702543" }
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.