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

1 Answer 1

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.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.