Thursday 20 April 2017

How I can get ID of a new record in mongoDB?

There is such a document structure in MongoDB

{
"_id" : ObjectId("58f7d556aa52ce456672a67e"),
"created" : ISODate("2017-04-19T21:23:34.315Z"),
"context_task" : [
    {
        "task" : "some text ",
        "status" : false,
        "_id" : ObjectId("58f7d559aa52ce456672a67f")
    }
],
"head" : {
    "userID" : "58f48037fc894e19a3f7b81b",
    "head_task" : "test record "
},
"__v" : 0
}

I add data to the context_task.task using the following query:

task.findOneAndUpdate({"_id": req.body.id_project},
            {$push: {'context_task': {'task': req.body.input_task,'status': false}}},{new: true},
            function (err, doc) {
                if (err) return next(err);
                var body = req.body.id_project+","+req.body.input_task;
                console.log(doc);
                res.status(200).send(body);
            });

Tell me, how can I get the context_task._id of a new record after inserting it? In this case, the entire document is returned.



via Вова Журавлёв

No comments:

Post a Comment