[SOLVED] How to update document in mongodb NodeJS?

Issue

This Content is from Stack Overflow. Question asked by Big Loot

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")
    }



Solution

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


This Question was asked in StackOverflow by Big Loot and Answered by Akash Sengar It is licensed under the terms of CC BY-SA 2.5. - CC BY-SA 3.0. - CC BY-SA 4.0.

people found this article helpful. What about you?