Why mongodb not giving me more than 100 documents?

2019-01-26 21:51发布

问题:

Why does my query not working with more than 100 documents in collection?

db.collection('allowedmacs').find().toArray(function(err, docs) {
console.log(docs);
}

err says this:

name: 'MongoError',
message: 'connection destroyed, not possible to instantiate cursor'

If documents <100 all works fine.

回答1:

You're probably doing something like this:

db.collection('allowedmacs').find().toArray(function(err, docs) {
  console.log(docs);
});
db.close();

So you're closing the database before the callback to toArray has been called (although it may work on some occassions).

Instead, try this:

db.collection('allowedmacs').find().toArray(function(err, docs) {
  console.log(docs);
  db.close();
});


回答2:

in source there is written:

MongoDB supports no more than 100 levels of nesting for BSON documents.

maybe you look into : Mongo and find always limited to 100 with geo data