我想只更新文档中新定义的值。如果未定义,请保留文档中的值。示例如下。
如果此文档存在于db
中{
"_id": "592c53b3bdf350ce004ad717",
"url": "hyoh7ryb-",
"fname": "Foo",
"lname": "Bar",
"email": "foobar@gmail.com",
"__v": 0,
"verfied": false,
"bio": {
"title": "Software Engineer",
"intro": "I like to code stuff.",
"summary": "Specializing in web development. Favorite language is Javascript.\n"
}
}
我的代码:
module.exports.updateBio = function(req, res) {
var userID = req.params.id;
var objForUpdate = {bio: {}};
if (!app.isEmptyString(req.body.profile_picture)) objForUpdate.bio.profile_picture = req.body.profile_picture;
if (!app.isEmptyString(req.body.title)) objForUpdate.bio.title = req.body.title;
if (!app.isEmptyString(req.body.intro)) objForUpdate.bio.intro = req.body.intro;
if (!app.isEmptyString(req.body.summary)) objForUpdate.bio.summary = req.body.summary;
if (!app.isEmptyString(req.body.skills)) objForUpdate.bio.skills = req.body.skills;
if (!app.isEmptyString(req.body.facebook)) objForUpdate.bio.social.facebook = req.body.facebook;
if (!app.isEmptyString(req.body.twitter)) objForUpdate.bio.social.twitter = req.body.twitter;
if (!app.isEmptyString(req.body.linkedin)) objForUpdate.bio.social.linkedin = req.body.linkedin;
if (!app.isEmptyString(req.body.website)) objForUpdate.bio.social.website = req.body.website;
var conditions = { _id: userID }
, setObj = { $set: objForUpdate }
, options = { multi: true };
Profile.update(conditions, setObj, (error, page) =>{
if(error) return res.status(500).json({ success: false, error: error });
res.status(200).json({ success: true, message: "Bio successfully updated." });
});
};
在我的身体发送请求中,我只更新标题,但我希望保留所有其他生物信息,而是为它们创建一个空白字段。
我该如何处理?
答案 0 :(得分:1)
嵌套对象(如bio)的设置语法是:
$ set:{' bio.profile_picture':20}
所以试试这个:
module.exports.updateBio = function(req, res) {
var userID = req.params.id;
var objForUpdate = {};
if (!troolr.isEmptyString(req.body.profile_picture)) objForUpdate['bio.profile_picture'] = req.body.profile_picture;
if (!troolr.isEmptyString(req.body.title)) objForUpdate['bio.title'] = req.body.title;
if (!troolr.isEmptyString(req.body.intro)) objForUpdate['bio.intro'] = req.body.intro;
if (!troolr.isEmptyString(req.body.summary)) objForUpdate['bio.summary'] = req.body.summary;
if (!troolr.isEmptyString(req.body.skills)) objForUpdate['bio.skills'] = req.body.skills;
if (!troolr.isEmptyString(req.body.facebook)) objForUpdate['bio.social.facebook'] = req.body.facebook;
if (!troolr.isEmptyString(req.body.twitter)) objForUpdate['bio.social.twitter'] = req.body.twitter;
if (!troolr.isEmptyString(req.body.linkedin)) objForUpdate['bio.social.linkedin'] = req.body.linkedin;
if (!troolr.isEmptyString(req.body.website)) objForUpdate['bio.social.website'] = req.body.website;
var conditions = { _id: userID }
, setObj = { $set: objForUpdate }
, options = { multi: true };
Profile.update(conditions, setObj, (error, page) =>{
if(error) return res.status(500).json({ success: false, error: error });
res.status(200).json({ success: true, message: "Bio successfully updated." });
});
};
答案 1 :(得分:0)
您应该像这样修改$ set运算符:
$set: {
'bio.title' : 'someinfo'
}
当你说:
var objForUpdate = {bio: {}};
这基本上意味着,您将擦除生物对象内的所有属性。