Thursday, 15 May 2014

mongodb - Javascript variable name coercion to string -



mongodb - Javascript variable name coercion to string -

i'm trying update database in mongo using string variable passed in. however, javascript automatically coerces variable type, , makes "type" key in database instead of string type points (let's "notify" instance)

"update_notifications": function (id ,type ,callback) { db.collection("backend_users", function(err, collection) { collection.update( {"_id": new mongodb.objectid(id)}, { $bit: { type : { xor: 1 } } }, function (err) { if (err) { console.log(color.red(err)); } callback(err); } ); }); },

is there anyway forcefulness mongo utilize "notify" instead of creating new "type" key? thanks!

you want build "update" object outside of statement this

"update_notifications": function (id ,type ,callback) { var update = { "$bit": { } }; update["$bit"][type] = { xor: 1 }; db.collection("backend_users", function(err, collection) { collection.update( {"_id": new mongodb.objectid(id)}, update, function (err) { if (err) { console.log(color.red(err)); } callback(err); } ); }); },

the "left" side considered string literal in object notation, can assign in manner shown

javascript mongodb coercion

No comments:

Post a Comment