1

I have a document stored in mongodb like this

"Fiction" : [
           {
                   "SNo": "1",
                   "authorName" : "Enid Blyton",
                   "bookName" : "Secret series",
                   "Amount" : 115
           },
           {
                   "SNo": "2",
                   "authorName" : "Enid Blyton",
                   "bookName" : "Mystery series",
                   "Amount" : 150
           }
   ]

I would like to update the amount from 115 to 135.

I tried to update the value like this.

db.fiction.update({"Fiction.SNo":"1"},{$set:{"Fiction.Amount":135}})

But I get a error.

The error is

cannot use the part (Fiction of Fiction.Amount) to traverse the element 

Can anyone help on this, I need to implement this to work from python.

1
  • db.fiction.update({"SNo":"1"},{$set:{"Amount":135}}) Commented Jan 5, 2016 at 6:50

2 Answers 2

1

Use the $ positional operator in your update as this identifies the element in an array to update without explicitly specifying the its position in the array:

db.fiction.update({"Fiction.SNo":"1"},{$set:{"Fiction.$.Amount":135}})
Sign up to request clarification or add additional context in comments.

Comments

1

The $ operator is used to iterate through the array list while updating.

Your query would be : db.Fiction.update({"Fiction.SNo":"1"},{$set:{"Fiction.$.Amount" : 135}})

Comments

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.