Andy
Andy

Reputation: 1

How to update object using where condition in CosmoDb with DocumentDb api

Using CosmoDb with MongoDb api from c# I can update a document with filter condition rather than using the Id. For example this code work fine with CosmoDb with MongoDb api.

        public bool UpdateTask(MyTask myTask)
    {
        var mongoCollection = GetBsonCollectionForEdit();
        var builder = Builders<BsonDocument>.Filter;

        var filterName = builder.Eq("Name", myTask.Name);
        var filterCategory = builder.Eq("Category", myTask.Category);
        var filter = builder.And(new[] { filterName, filterCategory });

        var replaceUpdate = Builders<BsonDocument>.Update;
        var ret = mongoCollection.ReplaceOne(filter, myTask.ToBsonDocument());

        return ret.ModifiedCount == 1;
    }

Can I do the same thing with CosmoDb using the SQL DocumentDb api ? (Without implement UDF or StoredPoc)

Thanks

Upvotes: 0

Views: 797

Answers (1)

Aravind Krishna R.
Aravind Krishna R.

Reputation: 8003

No, but you can perform a query for the criteria using CreateDocumentQuery, then iterate through the matching IDs and perform an update on each of them using ReplaceDocumentAsync.

Upvotes: 1

Related Questions