0

Here is the JSON file. I would like to return the total count of elements from the array named courseSchedule where _id: Zdhmwv5orgAd8pq54. It should return value: 4 How is the MongoDB query is like?

{
"_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"
    },

]
}

Hi ,Thanks for the help. I'm using meteorhacks:aggregate package Meteor.js app.Here is the code in server side.

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

It returns : [ { _id:'6CBXfkebKP2i9zxuZ', courseSchedule: [ [Object],[Object],[Object],[Object] ],numberOfCourseSchedule: 4} ]

My next question: How to access the property NumberOfCourseSchedule to get the value 4 as i need this number for the increment counter sooner or later. Thanks.

Daviv Ho
  • 61
  • 1
  • 1
  • 4

0 Answers0