0

After research, I found this link which shows you can update a field dynamically in mongoid:

Model#rename
Performs MongoDB's $rename modifier that renames a field atomically.

MONGOID 
person.rename(:bday, :dob) 

MONGODB COMMAND
collections["people"].update(   { "_id" : ... },   { "$rename" : {
"bday" : "dob" } } )

According to this stackoverflow post, when the fourth paraemeter is set to true, it should update all your records. But it is not working. When I do the following:

Contact.rename('apple info', 'new_info', false, true)

And then query mongodb:

db.mongoid_container_contacts.find()
{ "_id" : ObjectId("558c50256d6163b255060000"), "apple info" : "etretrytr", ...

As you can see, 'apple info' remains in the existing records. Why isn't the name of the existing records being updated?

Community
  • 1
  • 1
Donato
  • 2,727
  • 6
  • 29
  • 59

1 Answers1

0

There is no Contact.rename method in Mongoid4, that method seems to have gone away in Mongoid3. If you want to rename a field in Mongoid3+, you want to call rename on a query:

Contact.all.rename('apple info' => 'new_info')
mu is too short
  • 426,620
  • 70
  • 833
  • 800
  • Are you sure this is supposed to update current fields in the mongodb database? I tried it and it did not update existing fields in the database. – Donato Jun 30 '15 at 21:25
  • I just tried it and it worked for me. How are you checking that it didn't work? – mu is too short Jun 30 '15 at 21:40