0

I'm trying to update array element inside document.

The document looks like this:

{
    "_id": "11111ec6de08e20354634b1d",
    "firstname": "Israel",
    "lastname": "Lavi",
    "exams": [
        {
            "examId": "1000",
            "name": "something",
            "status": [
                {
                    "status": "created",
                    "date": "2018-11-09T08:01:46.627Z"
                }
            ]
        },
        {
            "examId": "2000",
            "name": "something",
            "status": [
                {
                    "status": "created",
                    "date": "2018-11-09T08:01:46.627Z"
                }
            ]
        }
    ]
}

I want to update exam where examId=1000 I'm using the following command:

collection.update({'_id': _id, 'exams': { $elemMatch : { 'examId': examId } } }, {$set: {'exams.$': lcExam}}, {upsert: true})

And getting the following error:

The positional operator did not find the match needed from the query.

Thanks in advance

Israel
  • 445
  • 2
  • 5
  • 15
  • 1
    Looks fine to me, except maybe Is that `examId = 1000` or `examId = "1000"`? Note that one is a string and therefore the only match. Either that or are you maybe using CosmosDB? – Neil Lunn Nov 19 '18 at 11:11
  • examId is string, I'm using Typegoose – Israel Nov 19 '18 at 11:18
  • 1
    That part kind of does not matter here. For mongoose the "schema" *might* have some importance, so you should include it in your question in order to ensure nothing is being rewritten incorrectly. As long as it's a string ( or the schema is indeed a string ) then it's fine. I asked about "CosmosDB" because that is a product which does "MongoDB emulation", but actually quite badly thus leading to reported errors like your one. So those are the things you are actually being asked. Cannot reproduce on vanilla MongoDB. – Neil Lunn Nov 19 '18 at 11:26
  • I found something that might be the cause, the object lcExam contains the exam json with examId in it. When I'm updating I'm passing examId that might be different then examId in the object (because I want to update the examId in the object also). but I still do not understand why I'm getting this error. If I use same examId then the update works ok – Israel Nov 19 '18 at 12:38
  • 1
    The error is because of the `"upsert"` option. The problem here is that the "document" may well exist, but the "array item" within the document may not be present. For this reason "upserts" don't really mix well as a pattern when you have arrays of sub-documents inside the documents. This means you need to be very careful and typically construct multiple statements in order to handle all cases. – Neil Lunn Nov 19 '18 at 12:58
  • 1
    There's some more detail you should read at [mongoDB upsert on array](https://stackoverflow.com/questions/22664972/mongodb-upsert-on-array) which talks about the process further. Due to it's age it shows the direct "command" format for invoking multiple updates which are now covered directly in the collection `bulkWrite()` method for every driver. – Neil Lunn Nov 19 '18 at 13:02
  • Thanks for your help, I will check this later today – Israel Nov 19 '18 at 17:22

0 Answers0