Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

(AZURE cosmosDB/mongoDB) Update fields of an specific element of an object in an array

{
    "_id" : ObjectId("59660d4099c1e682e0992ced"),
    "alpha" : [
        {
            "_id" : ObjectId("59660d4199c1e682e0992cee"),
            "number" : 1,
            "start_at" : ISODate("2017-07-12T11:51:28.895Z"),
            "end_at" : ISODate("2017-08-12T11:51:28.895Z"),
            "created_at" : ISODate("2017-07-12T11:51:29.346Z"),
            "updated_at" : null
        }
    ]
}

I have a document as above and I would like to update or change the field 'number' and 'updated_at' in a JSON element of 'alpha' array.

I tried this:

db.mycol.update({_id: ObjectId("59660d4099c1e682e0992ced"), alpha: {$elemMatch: {_id: ObjectId("59660d4199c1e682e0992cee")}}}, {$set: {"alpha.$.number": 2, "alpha.$.updated_at": new Date()}})

It works fine with normal mongodb server. But with azure cosmosdb/mongodb it says ok but doesn't really update the document. What am I doing wrong?

like image 500
Prata Avatar asked Jul 12 '17 12:07

Prata


Video Answer


1 Answers

update or change the field 'number' and 'updated_at' in a JSON element of 'alpha' array

Not all of MongoDB's query syntax / capabilities are implemented in Azure Cosmos DB: MongoDB API. Based on my test, It seems that Azure Cosmos DB: MongoDB API does not support Array Update now, this feature will be supported in future.

enter image description here

If possible, you can create an item on User Voice to give a feedback.

like image 152
Fei Han Avatar answered Oct 12 '22 13:10

Fei Han