两个字段上的mongoDB排序
我有一个mongodb文档,如下所示:两个字段上的mongoDB排序,mongodb,mongodb-query,aggregation-framework,Mongodb,Mongodb Query,Aggregation Framework,我有一个mongodb文档,如下所示: { "match_key" : 44801, "total_points" : 10.00, }, { "match_key" : 44901, "total_points" : 8.00, }, { "match_key" : 44901, "total_points" : 7.00, }, {
{
"match_key" : 44801,
"total_points" : 10.00,
},
{
"match_key" : 44901,
"total_points" : 8.00,
},
{
"match_key" : 44901,
"total_points" : 7.00,
},
{
"match_key" : 44901,
"total_points" : 11.00,
},
{
"match_key" : 44801,
"total_points" : 7.00,
}
db.collection.aggregate([
{
$match: {
match_key: {
$in: [
44801,
45910
]
}
}
},
{
$sort: {
total_points: -1
}
},
{
$sort: {
match_key: -1
}
}
])
我想对上述文档进行排序,但希望将具有相同匹配密钥的文档放在一起
实际结果:
{
"match_key" : 44901,
"total_points" : 11.00,
}
,
{
"match_key" : 44801,
"total_points" : 10.00,
},
{
"match_key" : 44901,
"total_points" : 8.00,
},
{
"match_key" : 44801,
"total_points" : 7.00,
}
{
"match_key" : 44901,
"total_points" : 6.00,
},
{
"match_key" : 44901,
"total_points" : 11.00,
},
{
"match_key" : 44901,
"total_points" : 8.00,
},
{
"match_key" : 44901,
"total_points" : 7.00,
},
{
"match_key" : 44801,
"total_points" : 10.00,
},
{
"match_key" : 44801,
"total_points" : 6.00,
},
但您会看到相同的匹配键不在一起
预期结果:
{
"match_key" : 44901,
"total_points" : 11.00,
}
,
{
"match_key" : 44801,
"total_points" : 10.00,
},
{
"match_key" : 44901,
"total_points" : 8.00,
},
{
"match_key" : 44801,
"total_points" : 7.00,
}
{
"match_key" : 44901,
"total_points" : 6.00,
},
{
"match_key" : 44901,
"total_points" : 11.00,
},
{
"match_key" : 44901,
"total_points" : 8.00,
},
{
"match_key" : 44901,
"total_points" : 7.00,
},
{
"match_key" : 44801,
"total_points" : 10.00,
},
{
"match_key" : 44801,
"total_points" : 6.00,
},
这样,具有相同匹配键的文档按降序排列
这就是我尝试做的:
db.collection.aggregate(
[
{ $match: { match_key:{$in:[44801,45910]}}},
{ $sort: {total_points:1,match_key:1} }
]
)
这是我想问的问题的sql等价物-
我在这里的主要目的是对文档进行排序,并将具有相同匹配键的文档按顺序放在一起
/**您需要将此
$sort
分为两个阶段,如下所示:
{
"match_key" : 44801,
"total_points" : 10.00,
},
{
"match_key" : 44901,
"total_points" : 8.00,
},
{
"match_key" : 44901,
"total_points" : 7.00,
},
{
"match_key" : 44901,
"total_points" : 11.00,
},
{
"match_key" : 44801,
"total_points" : 7.00,
}
db.collection.aggregate([
{
$match: {
match_key: {
$in: [
44801,
45910
]
}
}
},
{
$sort: {
total_points: -1
}
},
{
$sort: {
match_key: -1
}
}
])
测试:
注意:由于我们将$match
作为第一阶段,我们应该很好地进行排序,但即使我们需要进行两次排序,也要尽量保持数据集尽可能低,您需要在两个字段上设置两个总点数
和匹配键
问题:
因此,当您这样做时:{$sort:{total_points:-1,match_key:1}
您的查询首先对total_points:1
字段进行排序&如果它找到两个具有相同total_points
值的文档,那么它将转到match_key:1
对这两个文档进行排序,以保持它们的顺序
问题示例:使用如上所述的复合排序,您将得到以下结果
{
"_id": ObjectId("5a934e000102030405000002"),
"match_key": 44801,
"total_points": 10
},
{
"_id": ObjectId("5a934e000102030405000003"),
"match_key": 45910,
"total_points": 10
},
{
"_id": ObjectId("5a934e000102030405000005"),
"match_key": 44801,
"total_points": 7
}
如果您看到total_points
是按顺序排序的,第一优先级为total_points
&因为有两个文档具有“total_points”:10
它将在匹配键的第二优先级中启动。不管怎样,如果你想像11->10->7
那样排序,你需要使用降序机制-1
,而不是1
(它是升序的)。哇,这很有效:)。谢谢你的解释:)