我正在开发 sails.js 应用程序
应用模型
name: String,
address:String,
appSettings: {
header: String,
color : String,
font: String,
}
我在appSetting中使用独立的更新查询更新颜色和更新字体
我在这里添加了两个更新查询
对于 appSettings 中的 color 行
var data = {
appSettings:{
color : 'red'
}
};
var query = {_id:'xxxxxxxxxx'};
Application.update(query,data).exec(function(err,application) {
if (err) res.send(err);
res.send({message:'Success'});
});
用于 appSettings 中的 font 行
var data = {
appSettings:{
font : 'Times-New-Roman'
}
};
var query = {_id:'xxxxxxxxxx'};
Application.update(query,data).exec(function(err,application) {
if (err) res.send(err);
res.send({message:'Success'});
});
结果 JSON 文件是,
{
"name" : "Water Colors",
"address" : "No 25, Kandy Road, Colombo",
"appSettings": {
"font" : "Times-New-Roman"
}
}
问题是,当运行一个更新查询时,appSettings 被覆盖
如何只更新 appSettings 行中的字体行而不覆盖 appSettings 中的颜色行?
希望回答,如果不清楚请评论
最佳答案
您可以使用 native()
模型的方法来检索 native Mongo 驱动程序集合的基础实例,然后您可以执行原始 Mongo 查询/操作。原生<强>$set
运算符(operator)将按如下方式更新字段:
var data = {
appSettings:{
color : 'red'
}
};
var query = {_id:'xxxxxxxxxx'};
Application.native(function(err, collection) {
if (err) return res.serverError(err);
collection.update(
query,
{ "$set": data },
function (err, results) {
if (err) return res.serverError(err);
res.send({message: 'Success'});
}
);
});
关于javascript - 如何在 sails.js 中仅更新主行内的一个子行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35312750/