Docs Menu
Docs Home
/
Database Manual
/ /

Set 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

{ x: "hello" }

{ x: "hello", y: null }

{ y: "goodbye" }

{ x: null, y: "goodbye" }

{ z: "oops" }

{ x: null, y: null }

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

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
  • Can be used to set the missing key value to null only.

  • Can be performed inside or outside a transaction.

  • Can be performed as a retryable write or not.

  • For additional requirements, refer to the specific command/method.

update with
multi: false
  • Can be used to set the missing key value to null or any other value.

  • The update to set missing shard key fields 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

  • To set to a non-null value, refer to Change a Document's Shard Key Value.

  • For additional requirements, refer to the specific command/method.

  • Can be used to set the missing key value to null or any other value.

  • When setting missing shard key fields with a 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

  • Missing key values are returned when matching on null. To avoid updating a key value that is null, include additional query conditions as appropriate.

  • To set to a non-null value, refer to Change a Document's Shard Key Value.

  • For additional requirements, refer to the specific command/method.

  • To set to a null value, you can specify multiple shard key modifications in the bulk operation.

  • When setting missing shard key fields with a 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

  • To set to a non-null value, refer to Change a Document's Shard Key Value.

  • For additional requirements, refer to the underlying command/method.

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

Back

Change Shard Key Value

On this page