聚合功能在mongodb查询中不起作用

时间:2018-10-01 13:16:18

标签: mongodb mongodb-query

我是MongoDB的新手,我想在我想检查type == topic并获取以下输出的地方使用聚合函数

预期产量

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

示例输入db.GroupContents

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

示例输入db.PedagogyNodes

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

示例输入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"
}

到目前为止我尝试过的

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 (Dynamically checking)
                //$match:{type:"topic"} // Step 4 (Statically checking)
            },
            {
               $unwind:"$contentNodes.LearningNodes"
            },
            {
                $group:
                {
                    _id:"$contentNodes.LearningNodes",
                    count:{$sum:1}
                }
            }
        ])
      })
});

如果我点击RUN,我得到Script executed successfully, but there is no result to show.,这不是我的预期输出。请帮助

  

了解我的要求:

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

0 个答案:

没有答案