Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Node.js mongclient batchsize

I am having problem with while trying to fetch 10,000 rows from mongdb collection using mongodb native client. It works upto batchsize of around 9700 records but returns no data for any larger batchSize. Here is the code snippet .. .. Any ideas what preventing it??

app.get('/aps/allclients' , function(req,res) {

MongoClient.connect(url, function(err, db) {
  var collection = db.collection('allclients');
  collection.find({},{"batchSize":10000}).toArray(function(err, docs){ // works for batch size 9700 or less but not 10000
   
    res.send(JSON.stringify(docs));
  });
  db.close();
 });

});
like image 978
Ismail Siddiqui Avatar asked Dec 07 '25 11:12

Ismail Siddiqui


1 Answers

You're closing your connection before the async toArray completes.

Put the db.close() call inside the callback instead:

var collection = db.collection('allclients');
collection.find({}, {"batchSize":10000}).toArray(function(err, docs){   
  res.send(JSON.stringify(docs));
  db.close();
});

That said, you shouldn't be opening and closing your MongoClient connection pool on each request. Just open it when you app starts and close it when you're shutting it down.

like image 172
JohnnyHK Avatar answered Dec 10 '25 01:12

JohnnyHK