Search code examples
mongodbmongodb-queryaggregation-frameworkmongodb-aggregation

mongodb - Find count of values in other collection


I want to find how many "taskId" of below collectionOne is present in collectionTwo. Here, "taskId" in below collectionOne is embedded inside a array in a document.

Also, Let me know the different approaches to do this (if possible).

db.collectionOne.find({"sId":"DCNrnPeKFrBv" }).pretty()
{
        "_id" : "sX8o7mJhebs",
        "sId" : "DCNrnPeKFrBv",
        "data" : [
                {
                        "oId" : "7SycYQ",
                        "taskId" : 146108906
                },
                {
                        "oId" : "7SycYQ",
                        "taskId" : 14623846
                },
                {
                        "oId" : "fANQ",
                        "taskId" : 1461982
                },
                {
                        "oId" : "fAeNQ",
                        "taskId" : 131732
                },
                {
                        "oId" : "t6AF5yn",
                        "taskId" : 197681
                }
        ]
}



> db.collectionTwo.find().pretty().limit(2)
{
        "_id" : 146108906,
        "oId" : "7SycYQ",
        "name" : "ABC"
}
{
        "_id" : 1461982,
        "oId" : "fANQ",
        "name" : "XYZ"
}

In collectionTwo "_id" is equivalent to "taskId" of collectionOne.


Solution

  • Using the $lookup operator to do a left join on collectionTwo, you can get the counts as follows:

    db.collectionOne.aggregate([
        { $match: { sId: "DCNrnPeKFrBv" }},
        { $lookup: {
            from: "collectionTwo",
            localField: "data.taskId",
            foreignField: "_id",
            as: "tasksCount"
        } },
        { $addFields: { 
            tasksCount: { $size: "$tasksCount" }
        } }
    ])
    

    or if using older MongoDB server versions (below 3.2):

    db.collectionOne.aggregate([
        { $unwind: "$data" },
        { $lookup: {
            from: "collectionTwo",
            localField: "data.taskId",
            foreignField: "_id",
            as: "tasks"
        } },
        { $unwind: "$tasks" },
        { $group: {
            _id: "$tasks._id",
            count: { "$sum": 1 }
        } },
        { $group: {
            _id: null,
            tasksCount: { "$sum": "$count" }
        } }
    ])
    

    --EDIT--

    An alternative is to get a list of all the distinct taskIds in collectionOne, and use that list as count query on collectionTwo e.g.

    var taskIds = db.collectionOne.distinct("data.taskId");
    var tasksCount = db.collectionTwo.count({ "_id": { "$in": taskIds } });
    printjson(taskIds);
    printjson(tasksCount);