我是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);
});
};
答案 0 :(得分:2)
findByIdAndUpdate
可以接受选项对象作为第三个参数。
Model.findByIdAndUpdate(id, updateObject, {
new: true // get the modified document back
}, callback);
默认情况下,new
选项的值为false