Mongoid查找嵌入式文档 [英] Mongoid find embedded document
本文介绍了Mongoid查找嵌入式文档的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在尝试通过其ID搜索嵌入式文档,然后将其返回.据我所知,这可能是可行的,但是只能通过使用mongo查找嵌入该文档的文档,然后在ruby中搜索该文档来寻找我想要的嵌入式文档.像这样:
I'm trying to search for an embedded document by its id, and return it. This is possible, but only, as far as I see, by using mongo to find the document which embeds it, and then searching that document in ruby for the embedded document I'm after. Like this:
# commenter.rb
def post
# todo: find syntax do avoid double query
if user = User.any_of({'posts.commenter_ids' => self.id}).last
user.posts.where('commenter_ids' => self.id).last
end
end
看起来很简单,但是我没有在Google/SO搜索中找到我喜欢的东西.
Seems simple, but I haven't found anything I like obviously on google/SO search.
有想法吗?
推荐答案
class Order
embeds_many Products
end
class Product
embedded_in Order
end
prod_id = "1234" # the embedded doc's _id you request
o = Order.find(product_ids: prod_id)
p = o.products.find(prod_id)
另请参见是否查询Mongoid加载父文档后,嵌入式文档将命中数据库服务器
这篇关于Mongoid查找嵌入式文档的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文