Automattic / Automattic/mongoose

Memory spike when using `find()`

Open
#10,400 12 comments 0 reactions 0 assignees View on GitHub
performance
Dominant language
JavaScript
Stars
27.5k
Forks
4k
Avg merge
2d 7h
Merged PRs (30d)
35

Description

**Do you want to request a *feature* or report a *bug*?**
Report a bug

**What is the current behavior?**
When I make a `Model#find` call with an `$or` operator, not only does the operation take ~5 minutes, it also takes up ~1 GB memory.

**If the current behavior is a bug, please provide the steps to reproduce.**
My use-case:
```js
const result = await Guild.find({
$or: [
{ premium: { $eq: true } },
{ prefix: { $ne: 'p!' } },
{ blacklistedChannels: { $exists: true, $ne: [], $type: 'array' } },
],
});
```
Reproduction repo: https://github.com/almeidx/mongoose-memory-leak-repro

When I run `node index.js` on this repository:
![image](https://user-images.githubusercontent.com/42935195/123664162-02626f00-d82f-11eb-9d88-0a751dd7b2ef.png)

Not sure if relevant but, here are my collection details:
- documents: 8360
- size: 38.71MB

**What is the expected behavior?**
I expected it to not take ~5 minutes to process, but I can understand why it could take that long to process.
What I really didn't expect is the operation using ~1 GB of memory.

**What are the versions of Node.js, Mongoose and MongoDB you are using? Note that "latest" is not a version.**
`14.17.1`, `5.12.15` and `3.6.8`, respectively
(I'm not sure if by 'MongoDB' you meant the `mongodb` npm module or the version of the database. I am using MongoDB Atlas version `4.4.6`, `mongodb` npm module version `3.6.8`)

Contributor guide

Open the contributing guide

Research direction

Start with the linked reproduction repository and run node index.js using the reported Node.js 14.17.1, Mongoose 5.12.15, and MongoDB versions. Trace the Model#find call with the shown $or query and compare its runtime and memory behavior; done means identifying whether the behavior belongs to Mongoose or MongoDB and confirming a bounded-memory outcome or documented limitation.

Written by the indexing model from the issue text.

Assessment

Tech stack
javascript, mongodb, node.js
Domain
databases
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Needs clarification
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.