[英]Query in command must target single shard key
我有一个文档 ID 数组,我希望使用它来删除具有给定 ID 的文档。 文档 id 也是文档的分片键。 所以我为model.deleteMany(query)
提供了以下查询
查询: { doc_id: { '$in': [ 'docid1', 'docid2' ] } }
我仍然收到错误Query in command must target single shard key 。 是否可以在不遍历数组并逐个删除文档的情况下克服这个问题?
通过将集合中的任何文档与_id
字段匹配,您可以进行查询或删除命令:
db.collection.deleteMany({ _id: { $exists: true }})
在代码中指定模式 model 时,必须指定一个 Shard Key(分区键)。 一旦提供,我们可能会执行保存、更新和删除等操作
const mySchema = new Schema({
requestId: { type: String, required: true },
data: String,
documents: [{ docId: String, name: String, attachedBy: String }],
updatedBy: {
type: {
name: { type: String, required: true },
email: { type: String, required: true },
}, required: true
},
createdDate: { type: Date, required: true },
updatedDate: { type: Date },
}, { shardKey: { requestId: 1 } }
);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.