I have the following code:
connection((db) => {
db.collection(\'orders\')
.updateOne(
{ \"_id\": req.body
Use {$set: {"name": req.body.name}}
(as Sparw mentioned) to update the the properties you want in the document. Also, if the id that you pass as a parameter does not exists in the collection (and you want to create one with the same id) you can pass as a third parameter {upsert: true}
to create one.
In your example:
connection((db) => {
db.collection('orders')
.updateOne(
{ "_id": req.body._id}, // Filter
{$set: {"name": req.body.name}}, // Update
{upsert: true} // add document with req.body._id if not exists
)
.then((obj) => {
console.log('Updated - ' + obj);
res.redirect('orders')
})
.catch((err) => {
console.log('Error: ' + err);
}) })