Search code examples
mongodbmongodb-queryaggregation-frameworkmongodb-aggregation

Get all the distinct users through mongodb aggregation


I have an collection for messages something like below. Now i want to get all the distinct users to which ObjectId("57f55e4799aabf1c0565bc10") messages or receive message excluding itself (ObjectId("57f55e4799aabf1c0565bc10")). So how i can achieve it using aggregation.

  /* 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
}

I have tried but didn't able to get the required result as below :

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

 ]);

Expecting results like : -

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

Thanks for any suggestion.


Solution

  • You can use the $addToSet to add the senders and receivers to their respective sets(lists). Then you can use $setUnion to combine the sets. Then, you can use $setDifference to remove the current user.

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

    Sample output:

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