I use rest-api in Node and mongodb with a model. I run into problem when I update a document. The complete code looks like this
I have put a process.exit() at the end just to keep the problem isolated to the server.
When the statement process.exit() is reached I
can I see in mongodb(mlab) has made an update but in addition
the original document is still there.
So an update resulted in two documents one with the update and the other is the original
//Here is the schema model that I have exported
var atticThings = require("./atticModel.js")
router.put("/:id", function(req, res)
{
var updateObj = {};
if (req.body.title) updateObj.title = req.body.title;
if (req.body.label) updateObj.label = req.body.label;
if (req.body.position) updateObj.position = req.body.position;
if (req.body.wrapping) updateObj.wrapping = req.body.wrapping;
if (req.body.text) updateObj.text = req.body.text;
atticThings.findByIdAndUpdate({_id: new mongodb.ObjectID(req.params.id)}
, {$set: { updateObj }},
function(err, thing) {
if(err)
res.send(err);
console.log("Gör en exit");
process.exit();
})
});
I have looked here to find a solution
How to use mongoose to find by id and update with an example | ObjectRocket
I have google to find a solution to this but without luck
I have also looked in mongodb documentation and I do as it say in the dok
Here is a copy from mongodb(mlab) where I have two documents…
I updated several field with 111.
A funny thing is that the ObjectId that I really updated was this one 5ec68451dcacc085aca9927d but the one that was updated was 5ec68b214e96687e44141440
{
"_id": {
"$oid": "5ec68451dcacc085aca9927d"
},
"title": "skor",
"label": "sommarskor",
"position": "h3",
"text": "gamla och skorsASASDASD",
"wrapping": "Resegarderob",
"__v": 0
}
{
"_id": {
"$oid": "5ec68b214e96687e44141440"
},
"title": "1111111",
"label": "11111111111",
"position": "h3",
"text": "111111111111",
"wrapping": "Löst",
"__v": 0
}
//Alexandra