node.js - Concatenating variables in a mongodb update query -
this question has answer here:
- using variable in mongodb update 1 answer
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
Post a Comment