findByIdAndUpdate并推进mongodb问题?

时间:2017-08-04 07:06:42

标签: javascript arrays node.js

我是Mongodb的新手,我使用的是findByIdAndUpdate,它将更新文档但返回旧文档, 这是我的功能,

exports.createInner = function (req, res, next) {

    console.log("req", req.body);
    var innerFolderDetails = req.body.innerFolder;
    Repository.findByIdAndUpdate(innerFolderDetails._id, {
        $push: {
            innerFolder: {
                "foldername": innerFolderDetails.foldername,
                "ismainFolder": innerFolderDetails.ismainFolder,
                "parentfolderId": innerFolderDetails.parentfolderId,
            }

        }
    }, function (err, response) {
        if (err) return next(err);
        console.log(response);
        res.json(response);
    });
};

1 个答案:

答案 0 :(得分:2)

findByIdAndUpdate可以接受选项对象作为第三个参数。

Model.findByIdAndUpdate(id, updateObject, {
    new: true // get the modified document back
}, callback);

默认情况下,new选项的值为false