0

This is somewhat similar to my previous question How to update mongodb array values

"Fiction" : [
           {
                   "SNo": "1",
                   "authorName" : "Enid Blyton",
                   "bookSeries" : "Secret Seven series",
                   "bookName" : [{"Title": "Secret Seven Adeventure", "Amount": 115},
                                 {"Title": "Well done Secret Seven", "Amount": 135}]

           },
           {
                   "SNo": "2",
                   "authorName" : "Enid Blyton",
                   "bookSeries" : "Mystery series",
                   "bookName" : [{"Title": "Burnt Cottage", "Amount": 150},
                                 {"Title": "Vanished Prince", "Amount": 140}]
           }
   ]

I wish to update the Amount: 115 to Amount: 135.

From the solution to the earlier question I tried it by doing something like this

db.Fiction.update({"Fiction.SNo": "1","Fiction.bookName.$.Title": "Secret Seven Adeventure"},{$set:{"Fiction.bookName.$.Amount": 135}})

but I am getting WriteResult({ "nMatched" : 0, "nUpserted" : 0, "nModified" : 0 })

Can anyone tell me why I am getting like this

Edit:

The suggested answer works only if the index of the array is known.

But in my case the index of the array is not known.

5
  • Possible duplicate of Updating nested array inside array mongodb Commented Jan 5, 2016 at 10:13
  • @chridam The given solution is applicable only if the index is known but in my case the index is not known Commented Jan 5, 2016 at 10:39
  • As the embedded comment said, MongoDB doesn't support matching into more than one level of an array. Consider altering your document model so each document represents an operation, with information common to a set of operations duplicated in the operation documents. Furthermore, the solution provided an approach on how you can determine the indexes to use in the update, otherwise you are presently forced to modify your schema due to the $ positional update operator constraint - there's an open JIRA ticket for that here. Commented Jan 5, 2016 at 10:43
  • 1
    And iterating again: Overembedding is the root of all evil for MongoDB. You really want to simplify your model. Commented Jan 5, 2016 at 10:46
  • @MarkusWMahlberg Ok thanks. I will restructure my storing method Commented Jan 5, 2016 at 10:49

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.