我们有一个带有嵌入项目的模型条目:
class Entry
include Mongoid::Document
include Mongoid::Timestamps
include Mongoid::Spacial::Document
embeds_many :items, cascade_callbacks: true
...
class Item
include Mongoid::Document
include Mongoid::Timestamps
include Mongoid::Spacial::Document
embedded_in :entry
...
如果我直接通过项目 id 查询 mongo 的条目:
{"items._id" : ObjectId("50536b18baa072000f000360")}
它返回条目:
505363b36181ce00020006b1 {"created_at":"2012-09-14T17:04:51Z","items":[{"_id":"50536b1a2b17b3...
当我通过 Mongoid 查询时:
irb(main):002:0> Entry.where('items._id' => '50536b18baa072000f000360')[0]
=> nil
所有其他查询都有效(对于项目的其他字段和条目的字段)。但不是为了身份证。
我们正在运行 mongoid (2.4.12)。
最佳答案
显然你必须将 ID 包装在 BSON::ObjectId() 中,所以:
Entry.where('items._id' => BSON::ObjectId('50536b18baa072000f000360'))[0]
否则 mongo 会偶尔不返回结果。
关于ruby-on-rails - Mongoid 2.4 通过ID查询嵌入文档失败,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12430326/