Tyler James Tyler James - 1 year ago 29
Javascript Question

updateOne deleting my data [mongodb]

Alright, so I've been tinkering with this all night and I have to sleep so I'm asking here. Why is

updateOne
deleting my data when it should just be updating it?

DB.collection('users').updateOne({"name":"bob"}, {"age":"20"}, (e,i) => {

console.log(i);

});


i.result.ok
prints
1
, but when I go to search for
bob
after doing this, the data is gone entirely, vanished.

My first question is why is this happening, my second is how can I update, since apparently this isn't the way to do it.

I can run find({"name":"bob"}) just fine before hand, so the data does exist before running this.

After, it is gone. Help please, my blood pressure is getting way too high.

Answer Source

Try the following:-

You need to use $set to update specific fields. {} give blank, if there is no find query.

DB.collection('users').updateOne({"name":"bob"},{$set: {"age":"20"} });

To get your answers, refer mongodb-update.

Hope this will hep you solving the problem.