开发者

Mongoid / Mongodb and querying embedded documents

开发者 https://www.devze.com 2023-01-20 03:26 出处:网络
I have Author and Book models. An Author has many embedded Books. Can I quer开发者_StackOverflowy the embedded Books, or do I have to fetch Authors first to get Books?You can query embedded document

I have Author and Book models.

An Author has many embedded Books.

Can I quer开发者_StackOverflowy the embedded Books, or do I have to fetch Authors first to get Books?


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号