MongoDb db.Collection.update() deleted my document

Eddy picture Eddy · Jul 31, 2016 · Viewed 10.2k times · Source

I run an update statement on a mongodb database and it deleted my document! What did I do wrong?

This is the update statement:

db.Question.update( {Name: "IsSomeCompany"}, {ButtonValues: [0, 1, 2] } )

I am using Robomongo and the reply was Updated 1 record(s) in 22ms but when I checked the database the record was gone. What am I missing here?

Answer

Hamedz picture Hamedz · Aug 1, 2016

I use this syntax for updating multiple documents.

db.getCollection('YourDocument').update(
   { "matchingField" : "matchingValue"}, 
   { "$set": { "field": "value" } }, 
   { "multi": true } 
)

https://docs.mongodb.com/manual/tutorial/update-documents/