11

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.

Zabi
  • 845
  • 2
  • 9
  • 15

3 Answers3

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.

enter image description here

Hope it helps you.

richard
  • 12,263
  • 23
  • 95
  • 151
Jay Gong
  • 23,163
  • 2
  • 27
  • 32
  • Thanks for the answer Jay. As I have a shard key which is based on hash of a field, does it mean I have to query all the documents first, to get that field and then call delete for them with that field? – Zabi May 06 '18 at 17:56
  • @Zabi Yes, I think you need to query them first if you can't get them. – Jay Gong May 07 '18 at 02:29
  • thanks, that would make sense. It just that if I'm deleting millions of record that match a particular filter, I need to first query those millions records sharded field and then run delete. Any idea if there is a better way? – Zabi May 08 '18 at 02:58
  • 2
    what if I have another document {id:3,name:"a"} and I want to remove it based on the id, do I need to query both params, id (the one that identifies the document) and name (because it's a shard key), right? – nicks May 20 '18 at 08:35
0

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

enter image description here

Voodu
  • 770
  • 7
  • 18
Sachin Kalia
  • 1,027
  • 14
  • 24
0

Shard Key(Partition Key) has to be provided during specification of schema model in the code. Once its provided, we can perform regular operation like save, update and delete as usual.

Example:

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 } }
);

In the above code we specified requestId as Shard Key, now we can perform any mongo operations Example:

let request:any = await myModel.findById(requestId);
request.data ="New Data";
await request.save();

Hope that helps.

This works with all Mongo operations

Ganesh
  • 13
  • 4