javascript - How to upsert MongoCollection but keep some values? -
hi, don't know how put title i'm sure understand
so i'm trying set 2 fields on mongo collection when user click on button every 60sc have method running , make that:
upsertcollectionmachines = meteor.bindenvironment(function() { machinescount = 0; var protocol; var port; machine.list({ inspect: true }, meteor.bindenvironment(function(err, machines) { if (typeof machines === "undefined") { console.error("impossible access 'machines' 'machine.list: '" + err); } else { machines.foreach(meteor.bindenvironment(function(machineinfo) { machinescount += 1; if (machineinfo.url != null) { var urltoarray = machineinfo.url.split(":") port = urltoarray[urltoarray.length - 1]; protocol = "https"; } else { port = "2376"; protocol = "https"; } infosmachines.upsert({ namemachine: machineinfo.name, }, { namemachine: machineinfo.name, statemachine: machineinfo.state, ipaddr: machineinfo.driver.ipaddress, port: port, protocol: protocol //here want add isused: false, //whouseit: "", think if set isused true --> after 60s false again right ? }); })); if (apikeygenerated == false) { gettheapikeygrafana(function(err, res) { if (!err) { apikeygenerated = true; updatedashboard(); } else { console.error(err); } }); }else{ updatedashboard(); } } })); })
where important part infosmachines.upsert()
. want set 2 fields :
'lockthemachine': function(namemachine, nameuser){ infosmachines.upsert({ namemachine: namemachine, }, { namemachine: namemachine, isused: true, whouseit: nameuser }); //upsertcollectionmachines(); console.log(nameuser +" has locked machine: " + namemachine); },
but first doesn't likes add 2fields collection , secondly 60s interval method delete them right ?
thank
it little hard follow trying in question, think got main gist of it.
basically, want lockthemachine()
function upsert ever set namemachine
, isused
, , whouseit
fields , want upsertcollectionmachines()
set statemachine
, ipaddr
, port
, protocol
fields on update , set isused
, whouseit
default values on insert?
you should able using below upserts.
use upsert inside upsertcollectionmachines()
function.
infosmachines.upsert({ namemachine: machineinfo.name, }, { $set: { statemachine: machineinfo.state, ipaddr: machineinfo.driver.ipaddress, port: port, protocol: protocol }, $setoninsert: { isused: false, whouseit: "", } });
it set statemachine
, ipaddr
, port
, protocol
fields regardless if inserting or updated, modify isused
, whouseit
if inserting (therefore, not overwrite values set other function).
use upsert inside lockthemachine()
function.
infosmachines.upsert({ namemachine: namemachine, }, { $set: { isused: true, whouseit: nameuser } });
it ever insert or update 2 specific fields (isused
, whouseit
).
now, why work? first, using update operator expressions (instead of field:value
expressions) allow update specific fields when needed , using $setoninsert
ensures ever modify fields on insert only. note, if upsert determines needs insert new document, per mongodb upsert option behavior, creates new document using...
the fields , values of both
<query>
,<update>
parameters if<update>
parameter contains update operator expressions
this means don't have ever $set
namemachine
field explicitly in update expression.
Comments
Post a Comment