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.null。如果要将缺少的分片键值字段设置为非null值,请参阅更改文档的分片键值。
To perform the update, you can use the following operations on a 要执行更新,您可以在mongos:mongos上使用以下操作:
update withmulti: true | db.collection.updateMany() |
|
update withmulti: false | db.collection.replaceOne()db.collection.updateOne() |
|
findAndModify | db.collection.findOneAndReplace()db.collection.findOneAndUpdate()db.collection.findAndModify() |
|
db.collection.bulkWrite()Bulk.find.replaceOne()Bulk.find.updateOne()Bulk.find.update() |
|
Example示例
Consider a 考虑在sales collection which is sharded on the location field. Some documents in the collection have no location field. location字段上分片的销售集合。集合中的某些文档没有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值相同。要将这些字段显式设置为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()或其他只显式更新一个文档的方法设置缺少的分片键字段时,更新必须满足以下要求之一:
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更新的目标是单个分片