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();
});
});
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With