In my playing with Mongodb over the past few weeks I am developing more and more complex applications, in my latest project i would like to insert a key:value
into an object. I am using mongojs
& node.js
.
I am generating a dynamic key (ie: hash = 3gx83h7
) for my new key:value to be pushed into the object paths. I know I can do the following:
db.applications.update({'apikey':key,'uid':user._id}, {'$set':{'paths.hash':0}},function(err){
if(!err){}
});
But that will push a key:value similar to this:
paths:{
hash:0
}
How can i push in my generated hash as the key?
You need to assemble the key name programmatically so that hash
's value is used:
var setValue = {};
setValue['paths.' + hash] = 0;
db.applications.update(
{'apikey': key, 'uid': user._id},
{'$set': setValue},
function(err){
if(!err){}
}
);