Mongodb和查询嵌入式文档

Mongodb和查询嵌入式文档

本文介绍了Mongoid/Mongodb和查询嵌入式文档的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有AuthorBook模型.

Author具有许多嵌入式Books.

我可以查询嵌入式Books,还是必须先获取Authors才能获取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.

如果Author被定义为具有许多:books(并且book是嵌入的::文档)

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.

这篇关于Mongoid/Mongodb和查询嵌入式文档的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-18 18:04