Set Missing Shard Key Fields
On this page
If you have missing shard key fields, you can set the shard key field to
null
. If you want to set the missing shard key field to a
non-null
value, see Change a Document's Shard Key Value.
To perform the update, you can use the following operations on a
mongos
:
Command | Method | Description |
---|---|---|
update with multi: true |
| |
update with multi: false |
| |
| ||
|
Example
Consider a sales
collection which is sharded on the location
field. Some documents in the collection have no location
field. A
missing field is considered the same as a null value for the field. To
explicitly set these fields to null
, run the following command:
db.sales.updateOne( { _id: 12345, location: null }, { $set: { location: null } } )
When setting missing shard key fields with
db.collection.updateOne()
or another method that explicitly
updates only one document, the update must meet one of the following
requirements:
- the filter of the query contains an equality condition on the full shard key in the query
- the filter of the query contains an exact match on _id
- the update targets a single Shard