简体   繁体   中英

Mongoid / Mongodb and querying embedded documents

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 ?

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM