How to update only one substring inside the main line in sail.js file

I am developing a sails.js application

application model

name: String,
address:String,
appSettings: {
    header: String,
    color : String,
    font: String,
}

I am using an independent update request update color and update font in appSetting

I added an update request here

for string color inside 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'});
});

for the font string inside 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'});
});

Result JSON file ,

{
 "name" : "Water Colors",
 "address" : "No 25, Kandy Road, Colombo",
 "appSettings": {
    "font" : "Times-New-Roman"
 }
}

The problem is that when you run one request, the appSettings updates are overwritten

How to update font line inside appSettings line without overwriting color line in appSettings?

Hope answer if not clear pls comments

+4
2
 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'});
 });
0

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'});
        }
    );
});
+2

Source: https://habr.com/ru/post/1628100/


All Articles