Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Getting 'query in command must target a single shard'

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.

like image 261
Zabi Avatar asked May 01 '18 06:05

Zabi


1 Answers

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.

enter image description here

Hope it helps you.

like image 144
Jay Gong Avatar answered Oct 19 '22 17:10

Jay Gong