Error
Error Code: 146

MongoDB Error 146: Query Exceeded Memory Limit

📦 MongoDB
📋

Description

MongoDB Error 146 indicates that a database operation, typically a query or an aggregation pipeline stage, attempted to use more RAM than its configured limit. This error commonly occurs when processing large datasets or executing complex operations without sufficient memory resources or optimizations.
💬

Error Message

Exceeded Memory Limit
🔍

Known Causes

3 known causes
⚠️
Large Data Processing
Operations such as sorting, grouping, or joining large datasets can consume excessive memory within a single stage if not optimized.
⚠️
Aggregation Without Disk Use
Aggregation pipelines that don't specify `allowDiskUse: true` must keep all intermediate results in RAM, quickly hitting memory limits for large data.
⚠️
Unoptimized Query Plans
Queries or aggregation stages that cannot leverage indexes efficiently may scan and load too much data into memory during execution.
🛠️

Solutions

4 solutions available

1. Optimize Query and Indexes medium

Refine query logic and ensure appropriate indexes are in place to reduce data scanned.

1
Analyze the query causing the error. Identify fields used in `filter`, `sort`, and `group` stages. Use `explain()` to understand the query plan.
db.collection.find({ /* your query */ }).explain('executionStats')
2
Create indexes on the fields identified in the previous step. For compound queries, consider compound indexes.
db.collection.createIndex({ field1: 1, field2: -1 })
3
Rewrite the query to be more efficient. Avoid fetching more data than necessary. Use projection to select only required fields.
db.collection.find({ /* your query */ }, { fieldA: 1, fieldB: 1, _id: 0 })
4
If using aggregation, ensure the `allowDiskUse: true` option is set for operations that might spill to disk, although this error indicates memory limit, not necessarily disk spill.
db.collection.aggregate([ /* your pipeline */ ], { allowDiskUse: true })

2. Increase MongoDB's WiredTiger Cache Size medium

Allocate more RAM to MongoDB's WiredTiger storage engine cache.

1
Locate your MongoDB configuration file (e.g., `mongod.conf`).
2
Modify the `storage.wiredTiger.engineConfig.cacheSizeGB` parameter. The recommended value is typically 50% of available RAM for dedicated MongoDB servers, but can be adjusted based on system load.
storage:
  wiredTiger:
    engineConfig:
      cacheSizeGB: 4
3
Restart the MongoDB server for the changes to take effect.
sudo systemctl restart mongod

3. Limit Query Results easy

Reduce the amount of data returned by the query using `limit()` and `project()`.

1
If the query is not intended to return all matching documents, use the `limit()` method to restrict the number of returned documents.
db.collection.find({ /* your query */ }).limit(100)
2
Use projection to select only the necessary fields. This significantly reduces the memory footprint of the returned data.
db.collection.find({ /* your query */ }, { name: 1, email: 1, _id: 0 }).limit(100)

4. Tune System Memory and `ulimit` Settings advanced

Ensure the operating system has sufficient memory and that MongoDB processes have adequate virtual memory limits.

1
Monitor your system's overall memory usage. If the system is consistently low on RAM, consider adding more physical memory.
free -h
2
Check and increase the virtual memory limits for the MongoDB process user. This is often done by modifying `/etc/security/limits.conf`.
# Add these lines for the mongod user (or the user running mongod)
mongod   soft    as      unlimited
mongod   hard    as      unlimited
3
Apply the `ulimit` changes by logging out and logging back in as the MongoDB user, or by restarting the MongoDB service.
sudo systemctl restart mongod
4
Ensure the `nofile` limits are also adequate for the MongoDB user, as large datasets can lead to many open file handles.
# Add these lines for the mongod user
mongod   soft    nofile  65536
mongod   hard    nofile  65536
🔗

Related Errors

5 related errors