0

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"
    }
})
Ajay
  • 4,199
  • 4
  • 27
  • 47
  • The error comes from the fact that no part of the "query" portion of your update attempts to match a "specific" array member. Your ask however to update ALL array elements in a single request, presently cannot be done until MongoDB 3.6 is released and you actually have it. There are other ways to update ALL array members that would need to be applied. Most reasonable ones are provided as answers already. – Neil Lunn Nov 13 '17 at 06:31
  • And for full clarify, you actually asked the same question 2 days ago and you were linked to the same answer. The answer has not changed in 2 days. There is an example of the MongoDB 3.6 syntax there, and there are other approaches, being either to "iterate" for all elements or provide all possible indexes explicitly as the more palatable solutions. If there was a "different answer", then it would already be posted on the linked duplicate given to you twice now. You should learn from it instead of posting the same question. – Neil Lunn Nov 13 '17 at 06:43

0 Answers0