按相关文档的数量排序

时间:2018-01-26 01:19:20

标签: mongodb sorting

我有两个收集,身份和干预。身份有很多干预措施。我必须按三种类型排序:首先是有积极干预的身份,然后是非活动干预的身份,最后是没有干预的身份。每种类型都必须按字母顺序排序。

以下是我所追求的数据:

> 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添加其他值,但我不知道如何。

你有解决方案吗?

1 个答案:

答案 0 :(得分:1)

您可以在3.4中尝试以下聚合。

使用$lookup$addFields中使用$arrayElemAt获取匹配的干预数组(一对一),以输出第一个元素。

使用$indexOfArray在值列表中找到搜索到的布尔值的位置,该值仅为0或1,缺少字段为-1,后跟$addFields,以便将输出索引保留在额外字段中在文档中,在排序字段上降序$sort以对文档进行排序。

$project排除放弃排序字段和干预以获得预期的输出。

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}}
])