Mongoid / Mongodb and querying embedded documents

后端 未结 1 877
梦谈多话
梦谈多话 2020-12-24 12:22

I have Author and Book models.

An Author has many embedded Books.

Can I query the embedded Books

相关标签:
1条回答
  • 2020-12-24 13:04

    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 讨论(0)
提交回复
热议问题