Sign in
Log inSign up

Using cursor.batchSize() with db.collection.find()

Manish pamnani's photo
Manish pamnani
·Aug 21, 2019

I am trying to use cursor.batchSize() with db.collection.find()

const MongoClient = require('mongodb').MongoClient;

const url = 'mongodb://127.0.0.1';

const dbName = 'users'

MongoClient.connect( url,  ( err, client) => {

    var cursor = client.db(dbName).collection('exampleCOllection').find().batchSize(2);

    cursor.toArray()
        .then( (value) => {
            console.log(value)
        })
        .catch( (error) => {
            console.log(error)
        })


})

but it outputs all the results instead of the given batchSize i.e. '2'. I tried using {batchSize:2} after db.collection.find() but it didn't made any difference. Can anybody have a look and suggest anything?