【问题标题】:Query in command must target single shard key命令中的查询必须针对单个分片键
【发布时间】:2022-02-09 17:51:44
【问题描述】:

我有一个文档 ID 数组,我希望使用它删除具有给定 ID 的文档。文档 id 也是文档的分片键。所以我为model.deleteMany(query)提供了以下查询

查询: { doc_id: { '$in': [ 'docid1', 'docid2' ] } }

我仍然收到错误 Query in command must target single shard key。 是否可以在不遍历数组并一一删除文档的情况下克服这个问题?

【问题讨论】:

    标签: mongodb mongoose azure-cosmosdb azure-cosmosdb-mongoapi


    【解决方案1】:

    通过将集合中的任何文档与_id 字段匹配,您可以进行查询或删除命令:

    db.collection.deleteMany({ _id: { $exists: true }})
    

    在代码中指定模式模型的过程中,必须指定一个 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 } }
    );
    

    【讨论】:

      猜你喜欢
      • 2018-10-11
      • 1970-01-01
      • 1970-01-01
      • 2017-06-30
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-10-24
      相关资源
      最近更新 更多