How to Fix MongoDB Error 349 (KeyPatternShorterThanBound)
You trigger mongodb error 349 when a query or index bound has more fields than the key pattern. You fix it by matching your bound fields to the exact key pattern of the target index. In the database engine, this failure carries the official name KeyPatternShorterThanBound. It stops the operation before the server can read or change any data. Teams often see this issue during index checks, range queries, or shard chunk tasks. Root causes of mongodb error 349 in the database engine The databas
Your coding agents run with your access.
Claude Code reaches prod with a developer's credentials, and approval prompts get clicked through. hoop.dev checks every query, masks sensitive data, and holds risky changes for a human.
You trigger mongodb error 349 when a query or index bound has more fields than the key pattern. You fix it by matching your bound fields to the exact key pattern of the target index.
In the database engine, this failure carries the official name KeyPatternShorterThanBound. It stops the operation before the server can read or change any data. Teams often see this issue during index checks, range queries, or shard chunk tasks.
Root causes of mongodb error 349 in the database engine
The database engine uses key patterns to order data inside an index or shard key. A key pattern lists the exact fields that make up the index. A bound sets the start or end point for a scan across that index.
Error 349 happens when the bound defines values for more fields than the key pattern holds. For example, an index might use a single field for its key pattern. If a command sends a bound with two fields, the bound is longer than the key pattern.
The engine rejects this mismatch right away with KeyPatternShorterThanBound. It cannot compare extra bound values against index fields that do not exist. This often happens when an app sends a stale hint or a malformed range spec.
Schema changes can also cause this error in live clusters. A team might replace a compound index with a shorter index on the same collection. If an app still sends bounds built for the old compound index, the query fails.
Sharded clusters can hit this code during manual chunk splits or moves. If an admin passes a split point with extra fields, the shard rejects the command. The bound must match the shard key pattern field for field.
How to diagnose and fix the failure in production
Start by finding the exact query or command that triggered error 349 in your logs. Check the collection name, the index hint, and the bound fields sent by the client. Next, check the live indexes on that collection to see their true key patterns.
You do not need broad admin rights to inspect index definitions in production. In MongoDB, roles grant actions on resources down to a collection level. The built-in read role grants the listIndexes and listCollections actions on non-system collections.
MongoDB Authentication & Authorization + End-to-End Encryption: architecture patterns and best practices
Continue reading? Get the full guide.
Free. No spam. Unsubscribe anytime.
Give your on-call engineer the read role on the target database to inspect the indexes. Compare the fields in the live index against the bound in the failing command. Look for extra fields in the bound or a missing field in the index.
If the app sent the wrong bound, update the query code to remove the extra fields. Make sure the bound fields match the index key pattern in both count and order. Test the updated query in a staging database before you deploy it.
Sometimes the app is right, and the collection is missing a required compound index. In that case, you must create the proper index to match the bound. The readWrite role includes the createIndex and dropIndex actions needed for this fix.
Keep write access tight when you fix indexes on live systems. Only grant the readWrite role to the person or job running the index change. While engineers check query logs, data masking can rewrite emails, phone numbers, and card numbers in results.
How to prevent key pattern and bound mismatches
Most bound mismatches come from drift between app code and database indexes. Keep your index definitions in version control right next to your app queries. Review any change to a compound index before you run it in production.
Search your codebase for hard-coded index hints or manual range bounds before dropping an index. If you shrink a compound index, update all callers first so they send shorter bounds. Run automated tests against a fresh schema to catch error 349 before release.
Check MongoDB commands and documents on the wire
MongoDB roles and SCRAM login decide who opens a session. They do not check single commands or redact sensitive fields inside returned documents. With hoop.dev, you run an open-source proxy, a sidecar for your MongoDB, inline in the MongoDB wire protocol. On every command an AI agent or engineer runs against production, hoop.dev decides to pass, mask, block, or hold it for human review.
Before a command runs, hoop.dev guardrails check its effect and refuse banned drops or writes with MongoDB error code 13, or hold risky commands for approval. On the way back, hoop.dev data masking rewrites sensitive fields in returned documents in memory, and every verdict is logged. See the open-source code on GitHub.
Frequently asked questions
What does KeyPatternShorterThanBound mean in MongoDB?
KeyPatternShorterThanBound is the official name for error code 349 in the database engine. It means an operation passed an index or shard key bound with too many fields. The bound has more fields than the key pattern it references.
Which role do I need to check indexes when debugging error 349?
You only need the built-in read role on the target database to check indexes. The read role includes the listIndexes, listCollections, and collStats actions on non-system collections. You do not need write privileges just to view key patterns.
Does fixing error 349 require a cluster restart?
No, fixing this error never requires a database restart. You resolve it by removing extra fields from the query bound in your app. You can also fix it by creating the matching compound index with the createIndex action.
Related guides
In this post