如何更新sail.js中主行内的一个子行
我正在开发sails.js应用程序
应用模式
name: String, address:String, appSettings: { header: String, color : String, font: String, }
我在appSetting中使用独立更新查询更新颜色和更新字体
我在这里添加了两个更新查询
对于appSettings中的颜色行
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中的字体行
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中的颜色行?
希望答案,如果不明确的评论请
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'}); });
您可以使用模型的native()
方法来检索本地Mongo驱动程序集合的底层实例,然后您可以执行原始Mongo查询/操作。 本地$set
操作符将更新字段,如下所示:
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'}); } ); });