Mongoid / Mongodb and querying embedded documents

夙愿已清 提交于 2019-12-29 18:35:26

问题


I have Author and Book models.

An Author has many embedded Books.

Can I query the embedded Books, or do I have to fetch Authors first to get Books?


回答1:


You can query embedded documents, just qualify the name. Now, this will return all Authors that have books that match your query.

If Author is defined as having many :books (and book is an embedded::document)

@authors_with_sewid = Author.where("books.name" => "sewid").all

You'd then need to iterate over the authors and extract the books.



来源:https://stackoverflow.com/questions/3954520/mongoid-mongodb-and-querying-embedded-documents

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!