mongoid update not updating existing fields

时间:2015-06-25 19:15:52

标签: ruby-on-rails ruby mongodb mongoid

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?

1 个答案:

答案 0 :(得分:0)

Mongoid4中没有Contact.rename方法,该方法似乎已经在Mongoid3中消失了。如果要重命名Mongoid3 +中的字段,则需要在查询中调用rename

Contact.all.rename('apple info' => 'new_info')