6


I am trying to run an aggregation MongoDB and Nodejs, but I have some difficulties in running 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 am getting the expected output.
However, when I use the following little 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 am getting the error message: 'TypeError: Cannot read property 'toArray' of undefined'

Could somebody please help me?

Many thanks in advance, Andi

1
  • 2
    The above code doesn't give me the cannot read property error but it won't work because the reading is done asynchronously and after the connection is closed. Try removing the db.close line. Commented Sep 11, 2015 at 20:07

2 Answers 2

3

As @ExplosionPills correctly pointed out, your code won't work as the logging is done asynchronously and after the connection is closed so you could try removing the db.close() line or create a function that makes use of a callback function to returns the aggregation results:

var aggregateStates = function(db, callback) {
   db.collection('data').aggregate(
     [
         { $group: { "_id": "$State", "total": { $sum: 1 } } }
     ]
   ).toArray(function(err, result) {
         console.log(result);
         callback(result);
   });
};

Call the aggregateStates function:

var MongoClient = require('mongodb').MongoClient;
MongoClient.connect('mongodb://localhost:27017/weather', function(err, db) {
    aggregateStates(db, function() {
        db.close();
    });
});
Sign up to request clarification or add additional context in comments.

Comments

1

According to mongo-native driver doc, aggregate() returns null. Therefore, toArray() cannot be called from what it returns. However, aggregate() takes a callback which has the result of aggregate() if successful. So here is the revised code:

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}} }
    ], function(err, result) {
        if(err) {
            db.close();
            throw err;
        }
        console.log(result);
        db.close();
    });

});

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.