node.js - How to use a variable as an Object Key [MongoDB] -
this question has answer here:
i'm trying use variable name
given me function, setting object key
equal 'name'
function addlist(name, item, id){ // add user's list var $push = {}; $push[name] = item; console.log(id); db.collection('newcon').update({_id: id}, {"$push": {name: item}}); ret(id); }
other solutions similar don't work, since need have build onto array every time function called, not replace whatever in each time.
javascript has no nice notation creating object keys variables:
var $push_query = {}; $push_query[name] = item; ... {"$push": $push_query} ...
Comments
Post a Comment