Set Missing Shard Key Fields设置缺少的分片键字段

On this page本页内容

If you have missing shard key fields, you can set the shard key field to null. 如果缺少分片键字段,可以将分片键字段设置为nullIf you want to set the missing shard key field to a non-null value, see Change a Document's Shard Key Value.如果要将缺少的分片键字段设置为非null值,请参阅更改文档的分片键值

To perform the update, you can use the following operations on a mongos:要执行更新,可以在mongos上使用以下操作:

Command命令Method方法Description描述
update with multi: true使用multi: true更新db.collection.updateMany()
  • Can be used to set the missing key value to null only.可用于将缺少的键值仅设置为null
  • 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使用multi: false更新

db.collection.replaceOne()

db.collection.updateOne()

  • Can be used to set the missing key value to null or any other value.可用于将缺少的键值设置为null或任何其他值。
  • 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查询的筛选器包含与_id完全匹配的项
    • the update targets a single shard更新的目标是单个分片
  • To set to a non-null value, refer to Change a Document's Shard Key Value.要设置为非null值,请参阅更改文档的分片键值
  • For additional requirements, refer to the specific command/method.有关其他要求,请参阅特定命令/方法。
findAndModify

db.collection.findOneAndReplace()

db.collection.findOneAndUpdate()

db.collection.findAndModify()

  • Can be used to set the missing key value to null or any other value.可用于将缺少的键值设置为null或任何其他值。
  • 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查询的筛选器包含与_id完全匹配的项
    • the update targets a single shard更新的目标是单个分片
  • Missing key values are returned when matching on null. 匹配为null时返回缺少的键值。To avoid updating a key value that is null, include additional query conditions as appropriate.为了避免更新为null的键值,请酌情包括其他查询条件。
  • To set to a non-null value, refer to Change a Document's Shard Key Value.要设置为非null值,请参阅更改文档的分片键值
  • For additional requirements, refer to the specific command/method.有关其他要求,请参阅特定命令/方法。

db.collection.bulkWrite()

Bulk.find.replaceOne()

Bulk.find.updateOne()

Bulk.find.update()

  • To set to a null value, you can specify multiple shard key modifications in the bulk operation.要设置为null值,可以在批量操作中指定多个分片键修改。
  • 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查询的筛选器包含与_id完全匹配的项
    • the update targets a single shard更新的目标是单个分片
  • To set to a non-null value, refer to Change a Document's Shard Key Value.要设置为非null值,请参阅更改文档的分片键值
  • For additional requirements, refer to the underlying command/method.有关其他要求,请参阅底层命令/方法。

Example示例

Consider a sales collection which is sharded on the location field. 考虑在location字段上分片的sales集合。Some documents in the collection have no location field. 集合中的某些文档没有location字段。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:要将这些字段显式设置为null,请运行以下命令:

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:使用db.collection.updateOne()或其他只显式更新一个文档的方法设置缺少的shard键字段时,更新必须满足以下要求之一:

  • 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查询的筛选器包含与_id完全匹配的项
  • the update targets a single Shard更新的目标是单个分片
←  Change a Document's Shard Key ValueFind a Shard Key →