node.js - Concatenating variables in a mongodb update query -


this question has answer here:

i'm trying update specific field array using mongodb. so, i'm trying use following code:

  db.collection(cg).update(     {        _id : objectid(req.params.id)      },     {        $set: { "cg." + req.params.index + ".nom" : req.body.nom }      },      function (err, result){         res.json(result);     }   ); 

however, approach doesn't run. problem rises line:

$set: { "cg." + req.params.index + ".nom" : req.body.nom }.

if change line for:

$set: { "cg.0.nom" : req.body.nom }

it runs.

just notice... line i'm accessing 'cg' element have following structure:

as far know there solution (using projection) work on .find() method, can't find or adapt specific situation.

any tip/advice/new solution welcome, thank you.

try this:

    var field = "cg." + req.params.index + ".nom"      db.collection(cg).update(     {        _id : objectid(req.params.id)      },     {        $set: { field : req.body.nom }      },      function (err, result){         res.json(result);     }   ); 

Comments

Popular posts from this blog

sublimetext3 - what keyboard shortcut is to comment/uncomment for this script tag in sublime -

java - No use of nillable="0" in SOAP Webservice -

ubuntu - Laravel 5.2 quickstart guide gives Not Found Error -