Aggregate giving undefined object

Hi there,

Im hoping for some assitance, I have been trying to run and aggregate on my data but im not sure what to do with the result as it returns like so:

Aggregate {
   _pipeline: [ { '$group': [Object] } ],
   _model: Model { Kids },
   options: {} }

I’ve tried to toArray() and forEach it but it says the variable I stored it in is not a function. I’m trying to graph the results. My code below:

const provinces = Kids.aggregate([
		{
			$group: {
				_id: {
					province: '$province',
				},
				count: {
					$sum: 1,
				},
			},
		},
	]);

Hello @Emil_C, welcome to the MongoDB Community forum.

With NodeJS driver you can try this aggregation query:

const groupStage = 
  { 
    $group: {
        _id: {
            province: '$province',
        },
        count: {
            $sum: 1,
        },
    }
  };

collectionl.aggregate( [groupStage] ).toArray( ( err, docs ) => {
    assert.equal(err, null);
    console.log("Aggregation output:");
    console.log( JSON.stringify( docs ) );
    callback(docs);
});

The variable docs is an array with the aggregation output.

Also, refer the: