Mongoid 中的原子持久性与推送
我不明白Mongoid的原子方法推送。
我有这个文档:
class Campaign
include Mongoid::Document
field :messages, :type => Array # Array of hashes
end
现在在控制台中可以使用它,但消息它没有持久化。一个例子:
>> campaign = Campaign.last
=> #<Campaign _id: 4dc2b6617e296d53f000000d,...
>> data = {:from => '[email protected]'}
=> {:from=>"[email protected]"}
>> campaign.push(:messages, data)
=> [{:from=>"[email protected]"}]
日志现在说:
MONGODB blabla_development['campaigns'].update({"_id"=>BSON::ObjectId('4dc2b6617e296d53f000000d')}, {"$push"=>{:messages=>{:from=>"[email protected]"}}})
但是如果再次查询这个文档,消息字段为nil:
>> campaign = Campaign.last
=> #<Campaign _id: 4dc2b6617e296d53f000000d,...
>> campaign.messages
=> nil
我怎样才能持久化这个数据?
谢谢
I don't understand the atomic method push of Mongoid.
I have this document:
class Campaign
include Mongoid::Document
field :messages, :type => Array # Array of hashes
end
And now in the console a play with it but messages it's not persisted. An example:
>> campaign = Campaign.last
=> #<Campaign _id: 4dc2b6617e296d53f000000d,...
>> data = {:from => '[email protected]'}
=> {:from=>"[email protected]"}
>> campaign.push(:messages, data)
=> [{:from=>"[email protected]"}]
The log now says:
MONGODB blabla_development['campaigns'].update({"_id"=>BSON::ObjectId('4dc2b6617e296d53f000000d')}, {"$push"=>{:messages=>{:from=>"[email protected]"}}})
But if a query this document again, the messages field is nil:
>> campaign = Campaign.last
=> #<Campaign _id: 4dc2b6617e296d53f000000d,...
>> campaign.messages
=> nil
How can I persist this data?
Thanks
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您推送的不是数组,而是哈希。启用安全模式 mongoid (mongomapper) 如果您希望 mongodb 回答“成功”或“失败”,请改为“好吧,无论如何”。
要启用安全模式,请尝试此操作
或更改 config
在任何情况下在开发环境中都应为 true 。
要解决您的问题:
You are not pushing an array, but a hash. Enable safe mode mongoid (mongomapper) if you want mongodb to answer "successful" or "failed", instead of "ok, whatever".
to enable safe mode, try this
or change config
should be true in development environment in any case.
To fix your problem: