Error
Error Code: 61

MongoDB Error 61: Missing Shard Key Definition

📦 MongoDB
📋

Description

This error indicates that MongoDB cannot find a defined shard key for a collection during an operation within a sharded cluster. It typically occurs when a collection is not properly sharded or the shard key is referenced incorrectly.
💬

Error Message

Shard Key Not Found
🔍

Known Causes

3 known causes
⚠️
Collection Not Sharded
The target collection has not been properly sharded or its shard key was not defined using `sh.shardCollection()`.
⚠️
Incorrect Collection Reference
The operation is referencing a non-existent or misspelled collection name within the sharded cluster.
⚠️
Shard Key Mismatch
The operation attempts to use a shard key that doesn't match the one defined for the collection, or the key is malformed.
🛠️

Solutions

3 solutions available

1. Include Shard Key in Query medium

Add shard key field to query filter

1
Check shard key
sh.status()
2
Include shard key in query
// If sharded by user_id:
db.orders.find({ user_id: "123", status: "pending" })
3
For updates, include shard key
db.orders.updateOne(
  { _id: ObjectId("..."), user_id: "123" },  // Include shard key
  { $set: { status: "complete" } }
);

2. Use Multi Update for Scatter-Gather easy

When shard key unknown, use multi:true

1
Use updateMany for broadcast
db.orders.updateMany(
  { status: "pending" },
  { $set: { status: "cancelled" } }
);

3. Verify Document Has Shard Key easy

Ensure document contains shard key field

1
Check document structure
db.orders.findOne()
2
Insert must include shard key
// If shard key is user_id:
db.orders.insertOne({
  user_id: "123",  // Required!
  product: "item",
  total: 99.99
});
🔗

Related Errors

5 related errors