Chunk Range and Missing Shard Key Fields
Missing shard key fields fall within the same chunk range as shard keys
with null values. For example, if the shard key is on the fields { x:
1, y: 1 }
, then:
Document Missing Shard Key | Falls into Same Range As |
---|---|
|
|
|
|
|
|
Read/Write Operations and Missing Shard Key Fields
To target documents with missing shard key fields, you can use the
{ $exists: false }
filter condition on the shard key
fields. For example, if the shard key is on the fields { x: 1, y: 1
}
, you can find the documents with missing shard key fields by running
this query:
db.shardedcollection.find( { $or: [ { x: { $exists: false } }, { y: { $exists: false } } ] } )
If you specify a null equality match filter condition (e.g. { x: null
}
), the filter matches both those documents with missing shard
key fields and those with shard key fields set to null
.
Some write operations, such as a write with an upsert
specification, require an equality match on the shard key. In those
cases, to target a document that is missing the shard key, include
another filter condition in addition to the null
equality match.
For example:
{ _id: <value>, <shardkeyfield>: null } // _id of the document missing shard key
Set the Missing Shard Key Fields
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