0

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

Cmag
  • 14,946
  • 25
  • 89
  • 140

1 Answers1

4

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.

000
  • 26,951
  • 10
  • 71
  • 101