这是一个简单的模型。
class Event
include Mongoid::Document
field :name, type: String
field :schedule, type: String
field :description, type: String
field :active, type: Boolean, default: true
key :name
end
1.We创建和事件
ruby-1.9.2-p0 > event = Event.new(:name => "event1")
=> #<Event _id: event1, _type: nil, _id: "event1", name: "event1", schedule: nil, description: nil, active: true>
ruby-1.9.2-p0 > event.save!
=> true
2.Know让我们找到事件
ruby-1.9.2-p0 > event = Event.find("event1")
=> #<Event _id: event1, _type: nil, _id: "event1", name: "event1", schedule: nil, description: nil, active: true>
3.So更新事件属性
ruby-1.9.2-p0 > event.update_attributes!(:name => "new name")
=> true
4.让我们尝试找到事件
ruby-1.9.2-p0 > event = Event.find("new name")
Mongoid::Errors::DocumentNotFound: Document not found for class Event with id(s) new name.
5.没有找到,但旧版仍然存在
ruby-1.9.2-p0 > event = Event.find("event1")
=> #<Event _id: event1, _type: nil, _id: "event1", name: "event1", schedule: nil, description: nil, active: true>
我做错了什么?我希望这不是一个错误。
答案 0 :(得分:3)
我不相信MongoDB允许您更改_id
字段。当我使用标准mongo shell尝试它时,我收到此错误(意味着它不是Mongoid限制,它是实际Mongo软件的限制):
Mod on _id not allowed
每当您需要更改name
字段时,您可能需要: