如何累积数组元素的总数(MongoDB)

时间:2016-05-29 19:35:11

标签: mongodb mongodb-query

这是JSON文件。我想从名为courseSchedule的数组_id: Zdhmwv5orgAd8pq54返回元素的总数。 它应返回值:4 MongoDB查询是怎么样的?

{
"_id" : "Zdhmwv5orgAd8pq54",
"title" : "Learn ABC",

"courseSchedule" : [ 

    {
        "scheduleTitle" : "a",
        "scheduleAuthor" : "a",
        "id" : "93b817302efac52116794ec2"
    }, 
    {
        "scheduleTitle" : "b",
        "scheduleAuthor" : "b",
        "id" : "7651b1954a24d7c5a8bd58e1"
    }, 
    {
        "scheduleTitle" : "c",
        "scheduleAuthor" : "c",
        "id" : "43fbc406dca754596f8ea1d7"
    },
     {    
        "scheduleTitle" : "d",
        "scheduleAuthor" : "d",
        "id" : "43fbc406dca754596f8ea1d7"
    },

]
}

嗨,谢谢你的帮助。我正在使用meteorhacks:聚合包Meteor.js app.Here是服务器端的代码。

var pipeline = [
     {$project: 
      {courseSchedule:1, numberOfCourseSchedule: 
        { $size: "$courseSchedule" }
      }}];
     let result = course.aggregate(pipeline);
      console.log(result)

返回:[ { _id:'6CBXfkebKP2i9zxuZ', courseSchedule: [ [Object],[Object],[Object],[Object] ],numberOfCourseSchedule: 4} ]

我的下一个问题:如何访问属性NumberOfCourseSchedule以获取值4,因为我迟早需要增量计数器的这个数字。 感谢。

0 个答案:

没有答案