MongoDB 5.0 is end of life as of October 2024. This version of the documentation is no longer
supported. To upgrade your 5.0 deployment, see the MongoDB 6.0 upgrade procedures.
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:
For additional requirements, refer to the underlying
command/method.
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