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.

Community
  • 1
  • 1
Tony Roczz
  • 2,366
  • 6
  • 32
  • 59
  • Possible duplicate of [Updating nested array inside array mongodb](http://stackoverflow.com/questions/29634150/updating-nested-array-inside-array-mongodb) – chridam Jan 05 '16 at 10:13
  • @chridam The given solution is applicable only if the index is known but in my case the index is not known – Tony Roczz Jan 05 '16 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**](https://jira.mongodb.org/browse/SERVER-831). – chridam Jan 05 '16 at 10:43
  • 1
    And iterating again: [Overembedding is the root of all evil for MongoDB](http://blog.mahlberg.io/blog/2015/11/05/data-modelling-for-mongodb/). You really want to simplify your model. – Markus W Mahlberg Jan 05 '16 at 10:46
  • @MarkusWMahlberg Ok thanks. I will restructure my storing method – Tony Roczz Jan 05 '16 at 10:49

0 Answers0