skip to Main Content

I have a pretty large Mongo collection which I want to query in batches of 50K docs using a cursor with a pre-defined batchSize.

This is my code-

const aggregation = [{ $match: { example: '1' }}]

await mongoose.connect(CONNECTION_STRING)
const db = mongoose.connection
db.collection('some-collection')
    .aggregate(aggregation)
    .cursor({ batchSize: 50000 })
    .exec()

await cursor.eachAsync(async (doc) => {
  // do something
})

For some reason I keep getting the following error –

TypeError: db.collection(...).aggregate(...).cursor is not a function

What am I doing wrong? Is there any other way to query Mongo in batches?

2

Answers


  1. AFAIK there is no .cursor() function. You should specify batch size in the options parametr of .aggregate().

    const aggregation = [{ $match: { example: '1' }}]
    
    await mongoose.connect(CONNECTION_STRING)
    const db = mongoose.connection
    const cursor = db.collection('some-collection')
        .aggregate(aggregation, { cursor: { batchSize: 50000 } })
    
    await cursor.eachAsync(async (doc) => {
      // do something
    })
    
    Login or Signup to reply.
  2. None of the answers here worked for me (using Mongoose 6.3 on a ver 5.x MongoDB server) in 2022. According to their latest documentation this is what you have to do, and you don’t need to chain .exec() onto it anymore:

    const pipeline = [{...}, {...}]; // aggregate stages go here
    const cursor = Collection.aggregate(pipeline).cursor();
    
    // cursor.next() method call
    let doc;
    while ((doc = await cursor.next())) {
        console.dir(doc);
    }
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search