'TypeError: db.collection(...).aggregate(...).cursor is not a function when querying Mongo in batches

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?



Solution 1:[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
})

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 user14967413