Mongodb 按关联文档的编号排序
我有两个收集,身份和干预。身份有很多干预。我必须按三种类型分类:首先是积极干预的身份,然后是消极干预的身份,最后是没有干预的身份。每种类型都必须按字母顺序排序 以下是我的数据:Mongodb 按关联文档的编号排序,mongodb,sorting,Mongodb,Sorting,我有两个收集,身份和干预。身份有很多干预。我必须按三种类型分类:首先是积极干预的身份,然后是消极干预的身份,最后是没有干预的身份。每种类型都必须按字母顺序排序 以下是我的数据: > db.identities.find().pretty() { "_id" : ObjectId("56c2ba0377656280a1000008"), "screenname" : "User without intervention 1" } { "_id
> db.identities.find().pretty()
{
"_id" : ObjectId("56c2ba0377656280a1000008"),
"screenname" : "User without intervention 1"
}
{
"_id" : ObjectId("56c2ba0677656280a100000c"),
"screenname" : "User with inactive intervention 2"
}
{
"_id" : ObjectId("56c2ba49776562aca000076b"),
"screenname" : "User with active intervention 1"
}
{
"_id" : ObjectId("56c432fa776562356200011a"),
"screenname" : "User with active intervention 2"
}
{
"_id" : ObjectId("56c2ba49776562aca000076b"),
"screenname" : "User without intervention 2"
}
{
"_id" : ObjectId("56c42cc7776562209500006a"),
"screenname" : "User with inactive intervention 1"
}
> db.interventions.find().pretty()
{
"_id" : ObjectId("56c4448a7765622817000045"),
"identity_id" : ObjectId("56c2ba0377656280a1000008"),
"active" : true
}
{
"_id" : ObjectId("56c44a1b77656282f0000451"),
"identity_id" : ObjectId("56c42f54776562b78a00032f"),
"active" : false
}
{
"_id" : ObjectId("56c450c8776562336a000776"),
"identity_id" : ObjectId("56c432fa776562356200011a"),
"active" : true
}
{
"_id" : ObjectId("56c450c8776562336a000776"),
"identity_id" : ObjectId("56c42cc7776562209500006a"),
"active" : false
}
我希望得到以下结果:
{
"_id" : ObjectId("56c2ba49776562aca000076b"),
"screenname" : "User with active intervention 1"
}
{
"_id" : ObjectId("56c432fa776562356200011a"),
"screenname" : "User with active intervention 2"
}
{
"_id" : ObjectId("56c2ba49776562aca000076b"),
"screenname" : "User with inactive intervention 1"
}
{
"_id" : ObjectId("56c2ba0677656280a100000c"),
"screenname" : "User with inactive intervention 2"
}
{
"_id" : ObjectId("56c2ba0377656280a1000008"),
"screenname" : "User without intervention 1"
}
{
"_id" : ObjectId("56c2ba49776562aca000076b"),
"screenname" : "User without intervention 2"
}
我对MongoDb不是很在行。目前,我刚刚:
db['domain_test#identities'].aggregate({ $project: { screenname: { $toLower: "$screenname" } } }, {$sort: { screenname: 1 }})
它只按屏幕名称排序,但不检查其他表。我想我必须向$project
添加其他值,但我不知道如何添加
您有解决方案吗?您可以尝试下面3.4中的聚合 用于获取匹配的干预数组(一对一),并在
$addFields
中输出第一个元素
用于定位搜索的布尔值在值列表中的位置,该值仅为0或1,缺少字段为-1,后跟将输出索引保留在文档中的额外字段中,并在排序字段上降序以对文档进行排序
通过排除删除排序字段并进行干预以获得预期输出
db.identities.aggregate([
{"$lookup":{
"from":"interventions",
"localField":"_id",
"foreignField":"identity_id",
"as":"interventions"
}},
{"$addFields":{
"sortorder":{
"$let":{
"vars":{"intervention":{"$arrayElemAt":["$interventions",0]}},
"in":{"$indexOfArray":[[false,true],"$$intervention.active"]}
}
}
}},
{"$sort":{"sortorder":-1}},
{"$project":{"sortorder":0,"interventions":0}}
])
您的_id有重复项,您只需按
db.identies.find().sort({screenname:1})
您的mongo版本是什么?@Saravana很抱歉重复。这不仅仅是复制/粘贴,我编辑了数据。如果只是按屏幕名称排序在这种情况下起作用,那只是巧合。我希望先有积极干预的用户,然后是不积极干预的用户,然后是没有干预的用户。@Veeram我有mongo 3.4.9。