我有一个集合主题的文档,它具有以下嵌套数组
[
{
_id: ObjectId("618c6718c6a8cc3a84a0b37f"),
subscribers: [
{userId: ObjectId("618c643545f4ecd43e9b20f7"), points: {daily: 50, total: 100} },
{userId: ObjectId("618c641345f4ecd43e9b20f0"), points: {daily: 10, total: 80} },
{userId: ObjectId("618c648c45f4ecd43e9b20fc"), points: {daily: 20, total: 80} }
],
quizzes: [],
leaderboards: {
daily: [],
total: []
},
}
]是否按每日点对订阅者列表进行排序,并将结果设置为领导板字段中嵌套的每日数组?
也就是说,我希望我的文档在查询后会像这样
[
{
_id: ObjectId("618c6718c6a8cc3a84a0b37f"),
subscribers: [
{userId: ObjectId("618c643545f4ecd43e9b20f7"), points: {daily: 20, total: 70} },
{userId: ObjectId("618c641345f4ecd43e9b20f0"), points: {daily: 10, total: 60} },
{userId: ObjectId("618c648c45f4ecd43e9b20fc"), points: {daily: 15, total: 50} }
],
quizzes: [],
leaderboards: {
daily: [
{userId: ObjectId("618c643545f4ecd43e9b20f7"), points: {daily: 20, total: 70} },
{userId: ObjectId("618c648c45f4ecd43e9b20fc"), points: {daily: 15, total: 50} }
{userId: ObjectId("618c641345f4ecd43e9b20f0"), points: {daily: 10, total: 60} },
],
total: []
},
}
]我找到了以下代码
db.collection.aggregate(
{ $unwind: "$memberships" },
{ $sort: { "memberships.price": 1 }},
{ $group: {
_id: "$_id",
"memberships": { $push: "$memberships" }
}
})此聚合可以工作,但它对订阅者进行排序,而不是使用结果数组更新每日嵌套字段。
发布于 2021-11-11 04:39:37
你实际上是在正确的轨道上。您只需要使用$merge来更新回集合。
db.Topic.aggregate([
{
$unwind: "$subscribers"
},
{
$sort: {
"subscribers.points.daily": -1
}
},
{
$group: {
_id: "$_id",
"daily": {
$push: "$subscribers"
}
}
},
{
$project: {
leaderboards: {
daily: "$daily"
}
}
},
{
"$merge": {
"into": "Topic",
"on": "_id",
"whenMatched": "merge"
}
}
])这是供您参考的蒙戈游乐场
发布于 2021-11-11 05:16:21
@ray的答案是正确的,而且会工作得很好。
我将使用单个$set阶段重写查询,并使用$function操作符对嵌套字段进行排序。
db.collection.aggregate([
{
"$set": {
"leaderboards.daily": {
"$function": {
"body": "function(arr) {return arr.sort((a,b) => (a.points.daily < b.points.daily) ? 1 : ((b.points.daily > a.points.daily) ? -1 : 0));}",
"args": [
"$subscribers"
],
"lang": "js",
},
},
}
}
])https://stackoverflow.com/questions/69922954
复制相似问题