使用 Mongoid / Ruby 获取所有子文档

发布于 2024-12-25 17:47:21 字数 379 浏览 1 评论 0原文

我在 MongoDB 中有一个这样的文档结构:

User
 |---> Posts
        |----> Comments

因此,父文档 User 具有嵌入的 Post 文档集合,它本身具有嵌入的 Comment< 集合/代码> 文件。

我拿回了 User 文档,现在整个文档树都在内存中,如下所示:

user = User.where(username: 'test').first

user 中,我如何获取所有评论的列表?

I have a document structure in MongoDB like this:

User
 |---> Posts
        |----> Comments

So, a parent document User with a collection of embedded Post documents, which itself has a collection of embedded Comment documents.

I grab the User document back and now have the entire document tree in memory, like so:

user = User.where(username: 'test').first

From user how can I get a list of all the comments?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

画骨成沙 2025-01-01 17:47:21

尝试这样的事情:

user.posts.map{|p| p.comments}.flatten

或者

user.posts.flat_map(&:comments)

Try something like this:

user.posts.map{|p| p.comments}.flatten

Or

user.posts.flat_map(&:comments)
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文