I am trying to start aggregation of MongoDB and Nodejs, but I have some difficulties when starting the project. When I enter the following command in the MongoDB shell:
db.data.aggregate([{$match: {}},{$group: {'_id': '$State', 'total': {'$sum': 1}} }]).toArray()
then I get the expected result.
However, when I use the following small Nodejs program
var MongoClient = require('mongodb').MongoClient; MongoClient.connect('mongodb://localhost:27017/weather', function(err, db) { if(err) throw err; console.log("Connected correctly to server"); var col=db.collection('data'); col.aggregate([{$match: {}},{$group: {'_id': '$State', 'total': {'$sum': 1}} }]) .toArray(function(err, result) { if(err) throw err; console.log(result); }); db.close(); });
then I get an error: 'TypeError: Unable to read property' toArray 'from undefined'
Can someone please help me?
Thanks a lot in advance, Andi
source share