i have a document with one of the fields is an array of objects.
i updated one of the objects and called .save() on the document.
the promise is resolved and the returned document shows the updates (code below)
doc.save().then(savedDoc => {
console.log(savedDoc) // THE UPDATES ARE SHOWING HERE!! All good till here!
savedDoc === doc; // true });
now here is the strange part - the updates are not written down into the database!
what am I missing??
Hello @Sangram_Chahal, welcome to the MongoDB Community forum.
You need to pass the document to be inserted to the collection.save method. For example:
client.db("testdb").collection("testcoll")
.save(doc).then(writeResult => {
console.log(writeResult.result, writeResult.ops);
});
The output, in addition to inserting the document into the collection, prints:
{ n: 1, ok: 1 } [ <the inserted document> ]
Also, note the documentation says:
save is - Deprecated: use insertOne, insertMany, updateOne or updateMany
In your case, use insertOne
.
Also see these topics from the NodeJS Driver documentation: