1

I was going through question and it asked me calculate minimum value of score for each child in class. I wrote the following source code.
I am using Mongo 2.6.9 and Node v0.10.25 and ubuntu 14.04 LTS

var MongoClient=require('mongodb').MongoClient;
var server=require('mongodb').Server;

var mongoclient=new MongoClient(new server("localhost",27017));

mongoclient.connect("mongodb://localhost:27017/",function(err,db){
if(err) throw err;

var db=mongoclient.db('school');

cursor=db.collection('students').aggregate(
[
  {$match : {"scores.type" : "homework"}},
  {$unwind:"$scores"},
  {$group : {_id : '$name',
  'minimum' : { $min :"$scores.score"  }
}
}
]);
});

This aggregate query when run using node app.js gives this error

/home/oroborus/node_modules/mongodb/lib/mongodb/connection/base.js:246
        throw message;      
              ^
TypeError: object is not a function
    at /home/oroborus/node_modules/mongodb/lib/mongodb/collection/aggregation.js:317:7
    at /home/oroborus/node_modules/mongodb/lib/mongodb/db.js:1195:7
    at /home/oroborus/node_modules/mongodb/lib/mongodb/db.js:1903:9
    at Server.Base._callHandler (/home/oroborus/node_modules/mongodb/lib/mongodb/connection/base.js:453:41)
    at /home/oroborus/node_modules/mongodb/lib/mongodb/connection/server.js:488:18
    at MongoReply.parseBody (/home/oroborus/node_modules/mongodb/lib/mongodb/responses/mongo_reply.js:68:5)
    at null.<anonymous> (/home/oroborus/node_modules/mongodb/lib/mongodb/connection/server.js:446:20)
    at EventEmitter.emit (events.js:95:17)
    at null.<anonymous> (/home/oroborus/node_modules/mongodb/lib/mongodb/connection/connection_pool.js:207:13)
    at EventEmitter.emit (events.js:98:17)

Which according to me is coming from the aggregate function. But when i run the same query in mongo terminal i get a proper output.

cursor=db.students.aggregate( [   {$match : {"scores.type" : "homework"}},   {$unwind:"$scores"},   {$group : {_id : '$name', 'minimum' : {   $min :"$scores.score"  } } } ]);
{ "_id" : "Myrtle Wolfinger", "minimum" : 35.99397009906073 }
{ "_id" : "Gennie Ratner", "minimum" : 34.50565589246531 }
{ "_id" : "Nobuko Linzey", "minimum" : 19.27081566886746 }
{ "_id" : "Flora Duell", "minimum" : 40.68238966626067 }
{ "_id" : "Shin Allbright", "minimum" : 52.68629677727286 }

Ques- What and where is the mistake and how to rectify it .
Thanks.

1 Answer 1

5

The last argument of collection.aggregate() needs to be a callback. The mongodb driver is expecting a function but your last argument is an object. That's why you're getting that error. Here is the revised code with the callback:

var MongoClient = require('mongodb').MongoClient;
var server = require('mongodb').Server;

var mongoclient = new MongoClient(new server("localhost", 27017));

mongoclient.connect("mongodb://localhost:27017/", function(err, db) {
  if (err) throw err;

  var db = mongoclient.db('school');

  cursor = db.collection('students').aggregate(
    [
        {$match: {"scores.type": "homework"}},
        {$unwind: "$scores"},
        {
            $group: {
                _id: '$name',
                'minimum': {$min: "$scores.score"}
            }
        }
    ], function(err, result) {   // callback
        console.dir(result);
        db.close();
    }
  );
});
Sign up to request clarification or add additional context in comments.

2 Comments

That worked like a charm. I wandered like 10 post, if i wouldn't have posted it here, i would have never managed to get an answer. Thanks a ton. :)
can you answer this

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.