Folks, I am trying to use the $rename
operator ( http://docs.mongodb.org/manual/reference/operator/update/rename/ )
works:
collection.update( {_id: id}, {$rename: {'foo': 'bar'} } , function (err, result) {});
does not:
var this = 'foo';
var that = 'bar';
collection.update( {_id: id}, {$rename: {this: that} } , function (err, result) {});
Why am I not allowed to use variables in the mongoclient to specify things?
Thanks
Try like this:
var this = 'foo';
var that = 'bar';
var rename_query = {'$rename': {}};
rename_query['$rename'][this] = that;
collection.update( {_id: id}, rename_query , function (err, result) {});
The issue is that the object constructor {foo: bar}
, the key implies the quotation marks like {'foo': bar}
so it is not possible to use a variable there. But you can build the object separately like in my code example.
Also, beware that the variable this
is a special keyword. Do not use this
as a regular variable name. The syntax highlighting even has it in a different color! Please use a different name.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.