How to use aggregation function mongo db-query

做~自己de王妃 提交于 2019-12-20 07:29:08

问题


I am new in MongoDB and I would like to use the aggregation function where I want to check type == topic and get the following output

Expected output

[
    {
        conceptName : 59d98cfd1c5edc24e4024d00
        totalCount : 2
    },
    {
        conceptName : 59d98cfd1c5edc24e4024d03
        totalCount : 1
    }
]

Sample input db.GroupContents

{
    "_id" : "5a0948bb1c5edc7a5000521a",
    "type" : "topic",
    "groupID" : "5a0948bb1c5edc7a5000521a",
    "pedagogyID" : "59d98cfa1c5edc24e40249a3",
   }

Sample input db.PedagogyNodes

{
    "_id" : "59d98cfa1c5edc24e40249a3",
    "latestVersion" : "59d98cfa1c5edc24e402497f_1",
    "createdAt" : "2017-10-08 04:27:06",
    "updatedAt" : "2017-10-08 04:27:06"
}

Sample input db.PedagogyVersions

    {
    "_id" : "59d98cfa1c5edc24e402497f_1",
    "type" : "topic",
    "contentNodes" : {
        "LearningNodes" : [
            "59d98cfd1c5edc24e4024d00",
            "59d98cfd1c5edc24e4024d03",
            "59d98cfd1c5edc24e4024d00",
        ]
    },
    "createdAt" : "2017-10-08 04:27:06",
    "updatedAt" : "2017-10-08 04:27:06"
}

What I have tried so far

var groupID = "5a0948bb1c5edc7a5000521a"; // Step 1
var records;
var pnDoc;
var pvDoc;
db.GroupContents.find({groupID : groupID}).forEach(function (doc){ // Step 2
   var pedagogyID = doc.pedagogyID;
   var records = db.getSiblingDB('PedagogyService');
       records.PedagogyNodes.find({_id : pedagogyID}).forEach(function (pnDoc) { // Step 3
          var latestVersion = pnDoc.latestVersion;
          // addded aggregate function here
          records.PedagogyVersions.aggregate([
            {
                $match:{_id:latestVersion} // Step 4
            },
            {
               $unwind:"$contentNodes.LearningNodes"
            },
            {
                $group:
                {
                    _id:"$contentNodes.LearningNodes",
                    count:{$sum:1}
                }
            }
        ])
      })
});

I am unable to write db query based on my expected answer, please help.

Understand my requirement

Step : 1 => I am passing `groupID = 5a0948bb1c5edc7a5000521a`
Step : 2 => we have to check from GroupContents where groupID = groupID then we have to take `pedagogyID`
Step : 3 => we have to check from PedagogyNodes where _id = pedagogyID then we have to take `latestVersion`
Step : 4 => we have to check from PedagogyVersions where _id = latestVersion then we have to take `contentNodes->LearningNodes`
Step : 5 => Finally we have to do the aggregation then we have display the result

回答1:


Try to unwind the LearningNodes array and then count them by grouping them together

db.PedagogyNodes.aggregate([
    {
       $unwind:"$contentNodes.LearningNodes"
    },
    {
        $group:
        {
            _id:"$contentNodes.LearningNodes",
            count:{$sum:1}
        }
    }
])

In case you need to do any matches you can use the $match stage

db.PedagogyNodes.aggregate([
    {
        $match:{type:"topic"}
    },
    {
       $unwind:"$contentNodes.LearningNodes"
    },
    {
        $group:
        {
            _id:"$contentNodes.LearningNodes",
            count:{$sum:1}
        }
    }
])

Answering the edited question =>

You were not able to view the output on the console since mongoshell does not print script output on the screen. To do this, do the following:

var result =  records.PedagogyVersions.aggregate([......]);

result.forEach(function(resultDoc){
    print(tojson(resultDoc))
})



回答2:


To see the result of your aggregation you have to pass the callback to be executed as parameter.

records.PedagogyVersions.aggregate([
        {
            $match:{_id:latestVersion} // Step 4
        },
        {
           $unwind:"$contentNodes.LearningNodes"
        },
        {
            $group:
            {
                _id:"$contentNodes.LearningNodes",
                count:{$sum:1}
            }
        }
    ], function(err, results) {
         console.log(results);
    });


来源:https://stackoverflow.com/questions/52588025/how-to-use-aggregation-function-mongo-db-query

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!