Error
Error Code:
262
MongoDB Error 262: Operation Time Limit Exceeded
Description
MongoDB Error 262, 'Exceeded Time Limit', indicates that a database operation (such as a query, aggregation, or write) failed to complete within its configured or default time limit. This typically occurs when operations are complex, involve large datasets, or encounter performance bottlenecks.
Error Message
Exceeded Time Limit
Known Causes
4 known causesComplex or Inefficient Operations
Queries, aggregations, or write operations that are computationally intensive or process large volumes of data can exceed the default or configured time limits.
Missing or Ineffective Indexes
Without appropriate indexes, MongoDB may perform full collection scans, significantly increasing query execution time beyond the set limits.
Network Latency or Congestion
High network latency between the application and the MongoDB server, or network congestion, can delay operations and cause them to time out.
Server Resource Bottlenecks
Insufficient CPU, memory, or disk I/O on the MongoDB server can slow down operations, preventing them from completing within the specified time.
Solutions
3 solutions available1. Optimize Slow Queries medium
Identify and improve long-running queries that are exceeding the operation time limit.
1
Enable the slow query log to capture queries exceeding a specified threshold.
mongod --setParameter slowms=100 --logpath /var/log/mongodb/mongod.log
2
Analyze the slow query log for queries with high execution times. The logs will typically show the query itself and its execution time.
Example log entry:
2023-10-27T10:30:00.123+0000 I QUERY [conn123] query db.collection plan summary: IXSCAN { field: 1 } ; explain: { ... } ; cursorid: 12345 ; nscanned: 1000000 ; nreturned: 10 ; keysexamined: 1000000 ; executionStats: { executionSuccess: true, nreturned: 10, executionTimeMillis: 5000, totalKeysExamined: 1000000, totalDocsExamined: 1000000 } ; duration: 5000ms
3
Use `explain()` on the identified slow queries to understand their execution plan and identify bottlenecks. Look for full table scans, inefficient joins, or missing indexes.
db.collection.find({ field: 'value' }).explain('executionStats')
4
Add appropriate indexes to support the slow queries. For example, if a query filters on `field`, create an index on it.
db.collection.createIndex({ field: 1 })
5
Rewrite the query to be more efficient, if possible. This might involve changing the query logic or reducing the amount of data being processed.
N/A (requires query analysis and rewriting)
2. Increase Operation Timeout Settings easy
Adjust MongoDB server-side timeouts to allow longer execution for legitimate, complex operations.
1
Connect to your MongoDB instance using the `mongosh` shell.
mongosh
2
Set the `maxTimeMS` option for the specific operation. This is a client-side setting but can be applied to individual operations. A value of 0 means no time limit.
db.collection.find({ field: 'value' }).maxTimeMS(60000) // 60 seconds
3
Alternatively, for server-side adjustments (use with caution), you can set the `operationTimeoutMillis` parameter. This affects all operations. A value of 0 means no timeout. This is typically done during mongod startup or by updating the configuration file.
In mongod.conf (e.g., /etc/mongod.conf):
operationTimeoutMillis: 120000
4
Restart the `mongod` process for configuration changes to take effect.
sudo systemctl restart mongod
3. Monitor System Resources medium
Ensure the MongoDB server has sufficient CPU, memory, and disk I/O to execute operations promptly.
1
Use system monitoring tools like `top`, `htop`, `vmstat`, or cloud provider monitoring dashboards to observe CPU and memory utilization on the MongoDB server.
top
2
Monitor disk I/O performance using tools like `iostat`. High disk latency can significantly slow down database operations.
iostat -xz 5
3
Check MongoDB's internal metrics for performance. You can use `db.serverStatus()` or connect to MongoDB with a monitoring tool.
db.serverStatus()
4
If resources are consistently strained, consider upgrading your hardware, increasing RAM, or optimizing disk configurations (e.g., using SSDs).
N/A (hardware/configuration change)