replaceOne vs $set
update$set
These two look interchangeable and are not. One edits the fields you name; the other throws away everything you did not name.
Given:
{
_id: 101,
name: "Denish",
age: 21,
role: "admin"
}
Using:
db.users.replaceOne(
{ _id: 101 },
{
name: "New Name"
}
)
The document becomes essentially:
{
_id: 101,
name: "New Name"
}
You replaced the document.
Whereas:
db.users.updateOne(
{ _id: 101 },
{
$set: {
name: "New Name"
}
}
)
keeps everything else.