1

The below code is updating the database but is not returning the new document despite specifically stating 'returnNewDocument' to be true. I am not using mongoose but instead, I'm am using the MongoDB drivers as found here https://docs.mongodb.com/manual/reference/method/db.collection.findOneAndUpdate/

const { MongoClient } = require("mongodb");
...
const mongo = new MongoClient(process.env.MONGODB_CONNECTION_URI, {
  useUnifiedTopology: true
});
if (mongo) {
  mongo.connect();
  app.use(bodyParser.urlencoded({ extended: true }));
  app.use(cors());
  app.use(cookieParser());
  app.use(express.static(path.join(__dirname, "build")));
  app.listen(8080, () => console.log("Server running on 8080"));
}

app.get("/v1/editprofile", function(req, res) {
  const { givenName, familyName, mobile, address, serviceRadius, language }  = req.query;
  if (req.cookies && req.cookies.participant) {
    jwt.verify(req.cookies.participant, privateKey, { algorithms: ["RS256"] }, function(err, decoded) {
      if (err) res.status(400).send({ status: "logout", description: "Bad Cookie found" });
      

    async function insertProfile() {
        const id = decoded.id;
        const data = await mongo
        .db("Users")
        .collection("participants")
        .findOneAndUpdate(
          { id },
          {
            $set: {
              givenName, 
              familyName, 
              mobile, 
              address, 
              serviceRadius, 
              language
            }
          },
          { 
            upsert: true, 
            returnNewDocument: true 
          }
        );
        if (data) {
          const participant = data.value
          console.log(participant)
          res.status(200).send({ participant });
        }
      }


      insertProfile().catch(console.error);

    });
  } else {
    res.status(400).send({ status: "logout", description: "No Cookie found" });
  }
});
Bill
  • 4,614
  • 13
  • 77
  • 132
  • 1
    What library you using for mongo queries? Some libraries may have their own oprtions builder, so they may expect not `returnNewDocument`, but like in mongoose `new`..etc. So check library documentation – Mykola Borysyuk Oct 08 '20 at 11:42
  • Does this answer your question? [findOneAndUpdate used with returnNewDocument:true returns the Original document MongoDB](https://stackoverflow.com/questions/35626040/findoneandupdate-used-with-returnnewdocumenttrue-returns-the-original-document) – Alex Blex Oct 08 '20 at 11:43
  • Unfortunately, it references the node driver, not the MongoDB one as found here https://docs.mongodb.com/manual/reference/method/db.collection.findOneAndUpdate/#behavior – Bill Oct 08 '20 at 13:52
  • { new: true } remove upsert true and returnNew document just add above line – Kuldeep Mishra Oct 08 '20 at 14:02
  • I thought `new: true` is a mongoose thing – Bill Oct 08 '20 at 14:09
  • @Bill You are using the node driver, not the mongo shell. Your links point to mongodb shell documentation. – Alex Blex Oct 08 '20 at 15:09
  • I have updated my question to show the connection method – Bill Oct 08 '20 at 16:47
  • How does the update changes the question? Connection method doesn't matter. It's express app, i.e. nodejs. The only way to query mongodb from nodejs is by using native driver. – Alex Blex Oct 08 '20 at 17:20

1 Answers1

0

By official docs for mongo, you have to provide returnNewDocument: true explicitly, because it is returning by default old document, before it was updated.

Same thing if you use mongoose, but you just have to provide new: true in 3rd object. 1st one is find query, 2nd update query, and 3rd is extra options

Nenad Jovicic
  • 188
  • 1
  • 7