Home > Software engineering >  MongoDB | sort doesn't work with aggregate
MongoDB | sort doesn't work with aggregate

Time:04-21

I need to use aggregate to get distinct values, and they need to be sorted by a string. In this case, I have a collection with lots of documents about places. I need to write a code that outputs the first ten places that are located in "Bronx", sorted by the name of the place (from z to a) - and to show only the name of the place.

Sample of one document in JSON. There are many lookalikes but with different values:

{
  "borough": "Bronx",
  "cuisine": "Bakery",
  "name": "Champion Bakery",
  "restaurant_id": "40423830"
}

There are several duplicate names for the borough:Bronx, so I need to use aggregate to get only distinct values.

I tried to use different commands, but nothing works - I do get distinct values, but they tend to be random and not sorted. With find, the sorting does work. I checked various answers here, but nothing helped me.

I want the output to be sorted similarly to this but without duplicate values:

{ "name" : "Zaro'S Bread Basket" }
{ "name" : "Yolanda Pizzeria Restaurant" }
{ "name" : "Yankee Tavern" } 
{ "name" : "Yankee Jz Pizza" } 
{ "name" : "Woodlawn Cafe" } 
{ "name" : "Wild Asia" } 
{ "name" : "Wilbel Pizza" } 
{ "name" : "White Castle" } 
{ "name" : "White Castle" }
{ "name" : "White Castle" }

codes I tried outputs:

db.places.aggregate([
  { $match: { borough: "Bronx" } },
  { $group: { _id: "$name" } },
  { $sort: { name: -1 } },
  { $limit: 10 }
]).pretty()

output:

{ "_id" : "Morris Park Bake Shop" }
{ "_id" : "Roca Tone Seafood Restaurant" }
{ "_id" : "Rolen Bagels" }
{ "_id" : "Casa Promesa" }
{ "_id" : "Celeste'S Snack Bar" }
{ "_id" : "The Pizza Place" }
{ "_id" : "Bronx Grill" }
{ "_id" : "Pepe Joes Pizzeria" }
{ "_id" : "Ray'S Pizza Restuarant" }
{ "_id" : "Boulevard Tavern" }
db.places.aggregate([
  { $match: { borough: "Bronx" } },
  { $sort: { name: -1 } },
  { $group: { _id: "$name" } },
  { $limit: 10 }
]).pretty()

output:

{ "_id" : "Pepe Joes Pizzeria" }
{ "_id" : "Bronx Grill" }
{ "_id" : "Ray'S Pizza Restuarant" }
{ "_id" : "The Pizza Place" }
{ "_id" : "Rolen Bagels" }
{ "_id" : "Celeste'S Snack Bar" }
{ "_id" : "Casa Promesa" }
{ "_id" : "Boulevard Tavern" }
{ "_id" : "Morris Park Bake Shop" }
{ "_id" : "Roca Tone Seafood Restaurant" }   
db.places.aggregate([
  { $match: { borough: "Bronx" } },
  { $sort: { name: -1 } },
  { $group: { _id: "$name" } },
  { $sort: { name: -1 } },
  { $limit: 10 }
]).pretty()

output:

{ "_id" : "Morris Park Bake Shop" }
{ "_id" : "Roca Tone Seafood Restaurant" }
{ "_id" : "Rolen Bagels" }
{ "_id" : "Boulevard Tavern" }
{ "_id" : "Celeste'S Snack Bar" }
{ "_id" : "The Pizza Place" }
{ "_id" : "Bronx Grill" }
{ "_id" : "Pepe Joes Pizzeria" }
{ "_id" : "Ray'S Pizza Restuarant" }
{ "_id" : "Casa Promesa" }   

Thank you in advance!

CodePudding user response:

Your code works good. As you can see, you { $sort: { _id: -1 } then letter T will be on the top and letter B in the end.

db.collection.aggregate([
  {
    $match: {
      borough: "Bronx"
    }
  },
  {
    $group: {
      _id: "$name"
    }
  },
  {
    $sort: {
      _id: -1
    }
  },
  {
    $limit: 10
  },
  {
    $project: {
      _id: 0,
      name: "$_id"
    }
  }
])

output

[
  {
    "name": "Zaro'S Bread Basket"
  },
  {
    "name": "Yolanda Pizzeria Restaurant"
  },
  {
    "name": "Yankee Tavern"
  },
  {
    "name": "Yankee Jz Pizza"
  },
  {
    "name": "Woodlawn Cafe"
  },
  {
    "name": "Wild Asia"
  },
  {
    "name": "Wilbel Pizza"
  },
  {
    "name": "White Castle"
  },
  {
    "name": "The Pizza Place"
  },
  {
    "name": "Rolen Bagels"
  }
]

mongoplayground

  • Related