Switch parameters to the table update method {$ set: {& ldquo; parameters & rdquo;: data}}

advertisements
Comanda.findOneAndUpdate(id,{$set: {"orden.0.fondos.2.estadoitem": estado}}, {}, function(err, comanda)

I need to indirectly change that "2" in

$set: {"orden.0.fondos.2.estadoitem"

no matter what I do, i cant.. this will save my live =/..

i have tried everything, calling a var pos = 2; and then $set: {"orden.0.fondos.pos.estadoitem" also as a string pos= "2"; and nothing seems to work

thanks in advace


You can create $set field dynamically :

var set = { "$set": {} };
set.$set["orden.0.fondos." + pos + ".estadoitem"] = estado;

Comanda.findOneAndUpdate(id, set, {}, function(err, comanda) {

})