how to update document and an attribute of embedded documents array in mongo

时间:2017-11-13 06:23:00

标签: mongodb embedded-documents

I have an array of documents like this:

activity: [{
    "_id" : "0000a021d96b12e37828db6a583a9632",
    "user_guid" : "86f8dff81756ec760b9082d77753e51d",
    "start_date" : ISODate("2017-02-04T00:00:00.000Z"),
    "end_date" : ISODate("2017-12-22T00:00:00.000Z"),
    "type" : "integer_score",
    "target_value" : 3,
    "progresses" : [
      {
        "start_date" : ISODate("2016-11-25T00:00:00.000Z"),
        "end_date" : ISODate("2017-11-22T00:00:00.000Z")
      },
      {
        "start_date" : ISODate("2016-11-26T00:00:00.000Z"),
        "end_date" : ISODate("2017-11-23T00:00:00.000Z")
      },
      {
        "start_date" : ISODate("2016-11-22T00:00:00.000Z"),
        "end_date" : ISODate("2017-11-23T00:00:00.000Z")
      }
    ]
},
{
  //similar document
}]

I have multiple such activity documents in my DB. and each document has multiple embedded progresses. On providing activity IDS I want to update the start_date of both activity and all progresses to user_input_date.

I am trying with following query:

db.activities.update(  
{ "_id" : { $in: ["0000a021d96b12e37828db6a583a9632",  "828db6a583a96320000a021d96b12e37" ] } }, 
{ $set: { "start_date" : ISODate("2017-11-11T00:00:00.000Z"),  "progresses.$.end_date" :  ISODate("2017-11-11T00:00:00.000Z") }},
{multi: true }
);

But I am getting following error:

WriteResult({
    "nMatched" : 0,
    "nUpserted" : 0,
    "nModified" : 0,
    "writeError" : {
        "code" : 16837,
        "errmsg" : "The positional operator did not find the match needed from the query. Unexpanded update: progresses.$.end_date"
    }
})

0 个答案:

没有答案