通过mongodb聚合获取所有不同的用户

时间:2016-10-06 15:43:45

标签: mongodb mongodb-query aggregation-framework mongodb-aggregation

我有一个类似于下面的消息的集合。现在我希望得到所有不同用户的ObjectId(" 57f55e4799aabf1c0565bc10")消息或接收消息本身(ObjectId(" 57f55e4799aabf1c0565bc10"))。那么我如何使用聚合实现它。

  /* 1 */
{
    "_id" : ObjectId("57f55e0199aabf1c0565bc0e"),
    "message" : "fghjfghj",
    "to" : ObjectId("57f54e154df8d0193577889a"),
    "from" : ObjectId("57f5522099aabf1c0565bbfe"),
    "modifiedDate" : ISODate("2016-10-05T20:09:37.307Z"),
    "createdDate" : ISODate("2016-10-05T20:09:37.307Z"),
    "isDeleted" : false,
    "status" : true,
    "type" : "Message",
    "__v" : 0
}

/* 2 */
{
    "_id" : ObjectId("57f55e2d99aabf1c0565bc0f"),
    "message" : "ghjkfhkg",
    "to" : ObjectId("57f54e154df8d0193577889a"),
    "from" : ObjectId("57f5522099aabf1c0565bbfe"),
    "modifiedDate" : ISODate("2016-10-05T20:10:21.245Z"),
    "createdDate" : ISODate("2016-10-05T20:10:21.245Z"),
    "isDeleted" : false,
    "status" : true,
    "type" : "Message",
    "__v" : 0
}

/* 3 */
{
    "_id" : ObjectId("57f55e4799aabf1c0565bc10"),
    "message" : "hgkgh",
    "to" : ObjectId("57f54e154df8d0193577889a"),
    "from" : ObjectId("57f5522099aabf1c0565bbfe"),
    "modifiedDate" : ISODate("2016-10-05T20:10:47.614Z"),
    "createdDate" : ISODate("2016-10-05T20:10:47.614Z"),
    "isDeleted" : false,
    "status" : true,
    "type" : "Message",
    "__v" : 0
}

/* 4 */
{
    "_id" : ObjectId("57f55e7199aabf1c0565bc11"),
    "message" : "cbjngcfj",
    "to" : ObjectId("57f54e154df8d0193577889a"),
    "from" : ObjectId("57f5522099aabf1c0565bbfe"),
    "modifiedDate" : ISODate("2016-10-05T20:11:29.197Z"),
    "createdDate" : ISODate("2016-10-05T20:11:29.197Z"),
    "isDeleted" : false,
    "status" : true,
    "type" : "Message",
    "__v" : 0
}

/* 5 */
{
    "_id" : ObjectId("57f5801d94ef64d760275368"),
    "message" : "cbjngcfj",
    "from" : ObjectId("57f54e154df8d0193577889a"),
    "to" : ObjectId("57f5522099aabf1c0565bbfe"),
    "modifiedDate" : ISODate("2016-10-05T20:11:29.197Z"),
    "createdDate" : ISODate("2016-10-05T20:11:29.197Z"),
    "isDeleted" : false,
    "status" : true,
    "type" : "Message",
    "__v" : 0
}

/* 6 */
{
    "_id" : ObjectId("57f5865494ef64d76027536a"),
    "message" : "hgkgh",
    "to" : ObjectId("57f54e154df8d0193577889b"),
    "from" : ObjectId("57f5522099aabf1c0565bbfe"),
    "modifiedDate" : ISODate("2016-10-05T20:10:47.614Z"),
    "createdDate" : ISODate("2016-10-05T20:10:47.614Z"),
    "isDeleted" : false,
    "status" : true,
    "type" : "Message",
    "__v" : 0
}

/* 7 */
{
    "_id" : ObjectId("57f666de7d13f10b5aa53527"),
    "message" : "Zsdfsdf",
    "to" : ObjectId("57f54dd36df11022ac5d7769"),
    "from" : ObjectId("57f54dd36df11022ac5d7769"),
    "modifiedDate" : ISODate("2016-10-06T14:59:42.943Z"),
    "createdDate" : ISODate("2016-10-06T14:59:42.943Z"),
    "isDeleted" : false,
    "status" : true,
    "type" : "Message",
    "__v" : 0
}

/* 8 */
{
    "_id" : ObjectId("57f667397d13f10b5aa53528"),
    "message" : "HIIIIIIIIIIIII",
    "to" : ObjectId("57f54dd36df11022ac5d7769"),
    "from" : ObjectId("57f54dd36df11022ac5d7769"),
    "modifiedDate" : ISODate("2016-10-06T15:01:13.993Z"),
    "createdDate" : ISODate("2016-10-06T15:01:13.993Z"),
    "isDeleted" : false,
    "status" : true,
    "type" : "Message",
    "__v" : 0
}

/* 9 */
{
    "_id" : ObjectId("57f66972a533c40d67ebfab8"),
    "message" : "gfjghk",
    "to" : ObjectId("57f54e154df8d0193577889a"),
    "from" : ObjectId("57f5522099aabf1c0565bbfe"),
    "modifiedDate" : ISODate("2016-10-06T15:10:42.721Z"),
    "createdDate" : ISODate("2016-10-06T15:10:42.721Z"),
    "isDeleted" : false,
    "status" : true,
    "type" : "Message",
    "__v" : 0
}

我已尝试过,但无法获得所需的结果,如下所示:

db.getCollection('conversations').aggregate([
            {$match : {'type' : 'Message', 
                    $or : [
                            {from : ObjectId("57f5522099aabf1c0565bbfe")},
                            {to : ObjectId("57f5522099aabf1c0565bbfe")} 
                        ]
                      }
             },
             { $group: {_id :  {"to" : "$to", 'from':"$from"    }}},

 ]);

期待结果如: -

[
   ObjectId("57f54e154df8d0193577889a")
   ObjectId("57f54e154df8d0193577889b")
]

感谢您的任何建议。

1 个答案:

答案 0 :(得分:1)

您可以使用$addToSet将发件人和收件人添加到各自的集合(列表)中。然后,您可以使用$setUnion来组合这些集合。然后,您可以使用$setDifference删除当前用户。

db.getCollection('conversations').aggregate([
    {$match : {'type' : 'Message',$or : [ {from : ObjectId("57f5522099aabf1c0565bbfe")}, 
                                          {to : ObjectId("57f5522099aabf1c0565bbfe")}]}},    
    { $group: {_id :  null, from : {"$addToSet" : "$from"}, to : {"$addToSet" : "$to"}}}, 
    {"$project":{"userAll":{"$setUnion":["$from", "$to"]}}},
    {"$project":{"_id":0, users: {"$setDifference":["$userAll", [ObjectId("57f5522099aabf1c0565bbfe")]]}}}
]);

示例输出:

{
        "users" : [
                ObjectId("57f54e154df8d0193577889b"),
                ObjectId("57f54e154df8d0193577889a")
        ]
}