How to update document in mongodb NodeJS?

Viewed 36

I am using this command to update the document.

order = await db.collection("orders").findOneAndUpdate({ order_id: req.body.ORDERID }, {$set: { payment_status: "Paid", paymentInfo: JSON.stringify(myrequest) }})

console.log(order)

But the document is not updated instead return this in console

lastErrorObject: { n: 0, updatedExisting: false },
  value: null,
  ok: 1,
  '$clusterTime': {
    clusterTime: new Timestamp({ t: 1663565745, i: 13 }),
    signature: {
      hash: new Binary(Buffer.from("5165sd1vdsvds651vds5vvs5dvdsvdskvjdsv, "hex"), 0),
      keyId: new Long("1451151132154123165")
    }
1 Answers

To update the Document Using MongoDB two conditions are required filter and update information.

 const myquery = { orderId: req.body.ORDER_ID };
const newvalues = { $set: { payment_status: "Paid", paymentInfo: JSON.stringify('your Payment Info in JSON format') }};
  await db.collection("orders").updateOne(myquery, newvalues);

Please go through the Document for detailed Information

Related