Andrey Kon
Andrey Kon

Reputation: 797

Something is wrong with cursor

I try to fetch two documents one by one. I set batchSize to 1 for my test purposes. And I cant't fetch the second document. However, when I set batchSize to 2 it returns me the second doc without problems.

mongoClient.connect('mongodb://127.0.0.1:27017/mydb?w=1&journal=true&wtimeout=4000',server:{auto_reconnect:true, poolSize:2}}, function(err, db){
var cursor = db.collection('coll').find({},{},{batchSize:1});
    cursor.nextObject(function(e, doc)
    {
         console.log(doc);
         //We successfully fetched the first document and now it's time to get the second  one
         cursor.nextObject(function(e, doc2)
         {
            //It returns NULL!
            console.log(doc2);
         });
    });
});

Upvotes: 0

Views: 106

Answers (1)

Andrey Kon
Andrey Kon

Reputation: 797

I found the reason of this strange and silly behaviour. It happens to be intentional behaviour. http://docs.mongodb.org/manual/reference/method/cursor.batchSize/

Specifying 1 or a negative number is analogous to using the limit() method.

Upvotes: 1

Related Questions