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!

Community
  • 1
  • 1
RCN
  • 671
  • 1
  • 5
  • 17

1 Answers1

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".

Community
  • 1
  • 1
heinob
  • 19,127
  • 5
  • 41
  • 61
  • thank you so much! I had not seen that question/answer. Guess, I can write a python script to loop through. Thanks – RCN Feb 12 '14 at 21:07
  • I sort of figured it would only recognize that as a string, but I was uncertain how to reference the variable name – RCN Feb 12 '14 at 21:10
  • You can reference field names in the aggregation_framework (http://docs.mongodb.org/manual/aggregation/) using `$`. – heinob Feb 12 '14 at 21:12