Reputation: 1605
I'm using the mongodb-native-driver and I don't understand very well why they always set a batchSize=1 in the doc examples.
http://mongodb.github.io/node-mongodb-native/2.0/api/AggregationCursor.html#each
According to the offical doc, a batchSize equal to 1 shouldn't be used: https://docs.mongodb.org/v3.0/reference/method/cursor.batchSize/
What happens if I set a batchSize=1? Does it force to go to the database each time I retrieve a document?
It sounds really strange to me... The two references are official sources so I think I'm missing something.
Upvotes: 3
Views: 1661
Reputation: 775
you are refering to the mongo shell documentation - if you use cursor.batchSize(1) there, it will work same as limit().
The shell's behaviour is slightly different than the behaviour of a driver.
The example you show is from mongodb node.js driver (not from the mongo shell). It is ok to use batchSzie(1) there. It will force the driver to go to the database and pick up one document on each iteration. You can control the number of documents stored in memory this way. The higher the number, the fewer requests there are, from the client to the database, the more documents you store in client's memory, while iterating through them.
In the example you provided {cursor: {batchSize:1}} is one of the options of "aggregate" method. read the documentation here
Upvotes: 0