MongoDB |排序不适用于聚合
MongoDB | sort doesn't work with aggregate
我需要使用聚合来获取不同的值,并且需要按字符串对它们进行排序。
在这种情况下,我有一个集合,其中包含很多关于地点的文档。我需要编写一个代码,输出位于“布朗克斯”的前十个地点,按地点名称排序(从 z 到 a)- 并且只显示地点名称。
JSON 中一份文件的样本。有许多相似但具有不同值的对象:
{
"borough": "Bronx",
"cuisine": "Bakery",
"name": "Champion Bakery",
"restaurant_id": "40423830"
}
borough:Bronx 有多个重复名称,因此我需要使用聚合来仅获取不同的值。
我尝试使用不同的命令,但没有任何效果 - 我确实得到了不同的值,但它们往往是随机的并且没有排序。使用查找,排序确实有效。
我在这里检查了各种答案,但没有任何帮助。
我希望输出的排序方式与此类似,但没有重复值:
{ "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" }
我试过的代码 + 输出:
db.places.aggregate([
{ $match: { borough: "Bronx" } },
{ $group: { _id: "$name" } },
{ $sort: { name: -1 } },
{ $limit: 10 }
]).pretty()
输出:
{ "_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()
输出:
{ "_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()
输出:
{ "_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" }
提前致谢!
您的代码运行良好。如您所见,您 { $sort: { _id: -1 }
然后字母 T
将在顶部,字母 B
在最后。
db.collection.aggregate([
{
$match: {
borough: "Bronx"
}
},
{
$group: {
_id: "$name"
}
},
{
$sort: {
_id: -1
}
},
{
$limit: 10
},
{
$project: {
_id: 0,
name: "$_id"
}
}
])
输出
[
{
"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"
}
]
我需要使用聚合来获取不同的值,并且需要按字符串对它们进行排序。 在这种情况下,我有一个集合,其中包含很多关于地点的文档。我需要编写一个代码,输出位于“布朗克斯”的前十个地点,按地点名称排序(从 z 到 a)- 并且只显示地点名称。
JSON 中一份文件的样本。有许多相似但具有不同值的对象:
{
"borough": "Bronx",
"cuisine": "Bakery",
"name": "Champion Bakery",
"restaurant_id": "40423830"
}
borough:Bronx 有多个重复名称,因此我需要使用聚合来仅获取不同的值。
我尝试使用不同的命令,但没有任何效果 - 我确实得到了不同的值,但它们往往是随机的并且没有排序。使用查找,排序确实有效。 我在这里检查了各种答案,但没有任何帮助。
我希望输出的排序方式与此类似,但没有重复值:
{ "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" }
我试过的代码 + 输出:
db.places.aggregate([
{ $match: { borough: "Bronx" } },
{ $group: { _id: "$name" } },
{ $sort: { name: -1 } },
{ $limit: 10 }
]).pretty()
输出:
{ "_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()
输出:
{ "_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()
输出:
{ "_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" }
提前致谢!
您的代码运行良好。如您所见,您 { $sort: { _id: -1 }
然后字母 T
将在顶部,字母 B
在最后。
db.collection.aggregate([
{
$match: {
borough: "Bronx"
}
},
{
$group: {
_id: "$name"
}
},
{
$sort: {
_id: -1
}
},
{
$limit: 10
},
{
$project: {
_id: 0,
name: "$_id"
}
}
])
输出
[
{
"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"
}
]