-
Type: Bug
-
Resolution: Gone away
-
Priority: Unknown
-
None
-
Affects Version/s: 4.14.0
-
Component/s: None
What problem are you facing?
When .findOne({ field: "value" }) is called repeatedly, there is a constant increase in memory usage.
What driver and relevant dependency versions are you using
Using mongodb from mongoose.
- mongodb: (4.14.0)
- mongoose: (6.7.0)
- bullmq: (3.10.3)
Steps to reproduce?
import { Worker } from 'bullmq' import mongoose from 'mongoose' const client = await mongoose.connect(url) const collection = client.connection.db.collection('collectionName') new Worker('some-queue', async function() { const document = await collection.findOne({ field: "someValue" }) })
My research:
Looking at the source code for .findOne() I see it is calling
.find().limit(-1).batchSize(1).next()
However, in the mongodb documentation for .batchSize() it mentions to not to specify size of 1:
https://www.mongodb.com/docs/manual/reference/method/cursor.batchSize/
My temporary solution is to .find() without .batchSize(1):
.find().limit(-1).next() // ".findOne()" without memory leak
Memory usage with .findOne()
With .find().limit(-1).next()
- related to
-
NODE-5187 driver does not close cursor when `initialize` returns a single document
- Closed