node.js - How to add new items to an array in MongoDB -
i'm trying add new item whichever name passed in under whichever id. first problem seems not grabbing values of variables (name, item, id), instead using them object keys. next issue when tested hard-coding sample table info onto here, wasn't adding more items array, replacing entire array whatever values had here.
function addlist(name, item, id){ // add user's list console.log(id); db.collection('newcon').update({_id: id}, { "$set": { "$push": { name:item } } }); ret(id); }
$set
not array update operation.
the $set operator replaces value of field specified value.
you want use $push
itself, in
.update({_id: id}, {$push: {name: item}})
you can't interpolate object property names in raw object declarations, if want use variable name
have create object this:
var obj = {}; obj[name] = item;
you can pass .update
, {$push: obj}
Comments
Post a Comment