2

I am not at all familiar with javascript and was hoping I could receive some help with a mongo update of a variable.

I have looked at the following question : Pass variables into mongo updates?

but was confused a bit with the looping and the syntax.

I have a field called "name": "MyName" I am looking for a way to get the following results using a mongo shell command: "name": "MyName Is" with a query as well

How would I do that? Is this a completely incorrect approach?

db.collection.update({"old_id":{$regex:".*_change.*"}}, {$set:{"name":"name" + " Is"}}, {multi:true})

Thanks!

1
  • Looks to be correct, have you tried it? Does it produce the desired results? Commented Feb 12, 2014 at 20:59

1 Answer 1

2

The simple answer is: You cannot! You cannot update a field using another field. So you cannot update a field using itself. See also here.

BTW:

This

 $set:{"name":"name" + " Is"}

will set the name property to "name Is".

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

3 Comments

thank you so much! I had not seen that question/answer. Guess, I can write a python script to loop through. Thanks
I sort of figured it would only recognize that as a string, but I was uncertain how to reference the variable name
You can reference field names in the aggregation_framework (docs.mongodb.org/manual/aggregation) using $.

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.