Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

updateOne deleting my data

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.

like image 953
Tyler James Avatar asked Jan 22 '26 19:01

Tyler James


1 Answers

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.

like image 174
Shrabanee Avatar answered Jan 24 '26 08:01

Shrabanee