Getting 'query in command must target a single shard'

…衆ロ難τιáo~ 提交于 2020-02-21 12:44:45

问题


I have a CosmosDB setup using the Mongo API. I have a collection with a hashed shard on one of the field of the document. When I run commands like db.collection.remove or db.collection.deleteMany I get the following error.

Command deleteMany failed: query in command must target a single shard key.: {"message":"Command deleteMany failed: query in command must target a single shard key."}

I'm not sure how can I mention a shard key as part of the query considering I want the query to run across all the shards.


回答1:


You need to provide shard key when you want to run commands like db.collection.remove or db.collection.deleteMany.

For example :

My data source as below:

[
    {
        "id" : "2",
        "name" : "b"
    },
    {
        "id" : "1",
        "name" : "a"
    }
]

And my shared key is "/name". Use db.coll.deleteMany({"name":"a"}) to delete specific shard.

Hope it helps you.




回答2:


It should be ShardKey which you have chosen when you created cosmosDb collection.

FilterDefinition<Product> filter = Builders<Product>.Filter.Eq("id", 2);
=== 2 is my shardKey

await this._dbContext.GetProducts.DeleteOneAsync(filter);
return RedirectToAction("Index");

Kindly refer an image below , how does it look like in CosmosDB



来源:https://stackoverflow.com/questions/50112253/getting-query-in-command-must-target-a-single-shard

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!