根据父文档的属性查找文档

发布于 2024-11-29 06:04:04 字数 407 浏览 1 评论 0原文

假设我有两个收藏、帖子和评论。

post = { _id: 1,
         language: 'en',
         title: ...,
         author: ... }

comment = { _id: 2345,
            parent: 1,
            title: ...,
            author: ... }

除了查找所有评论,然后查询其父属性以找出语言之外,是否还有另一种方法来查找父级语言为“en”的所有文档(评论)?

如果没有,是否有办法以不同的方式呈现文档以使此类查询更容易执行? (在这种情况下,我为评论创建了一个不同的集合,而不是每个帖子一个文档,因为在我看来,获取一条评论比检索整个文档并过滤它更容易,也更有效)。

Supposing I have two collections, posts and comments.

post = { _id: 1,
         language: 'en',
         title: ...,
         author: ... }

comment = { _id: 2345,
            parent: 1,
            title: ...,
            author: ... }

Is there another way to find all the documents (comments) whose parent has the language 'en' apart from finding all the comments, and then querying on their parent attribute to find out the language?

If not, is there a way to present the document differently to make such a query easier to perform? (In that case, I made a different collection for the comments, instead of a single document per post, because it is easier and in my mind more efficient to fetch one comment, than retrieving the whole document and filtering it).

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

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

发布评论

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

评论(1

无法回应 2024-12-06 06:04:04

首先,做你想做的事没有捷径。所以你的第一个问题的答案是否定的。

对于 NoSQL 数据库来说,规范化并不是很重要。在本例中,您想根据语言代码查询评论,因此我只需将语言代码也添加到评论中即可。您知道要在应用程序端添加评论的帖子,因此您可以根据需要简单地存储帖子的语言。

至于你的架构;如果您经常显示单个评论,则每个评论都有一个文档可能是您的最佳解决方案。在大多数其他情况下,您总是至少显示大量评论,在这种情况下,通常最好为每个文档收集评论。每个评论页面说一个文档。也可以实现非常快速的分页。如果您不希望每个帖子有很多评论,您可以嵌入它们。

First of all, there is no shortcut to do what you want. So the answer to your first question is no.

With NoSQL databases normalization is not very important. In this case you want to query comments based on language code so I'd simply add the language code to the comment as well. You know for which post your adding a comment app-side so you can simply store the post's language if you want.

As for your schema; having a document per comment may be the best solution for you if you regularly show single comments. In most other situations you always show at least a significant amount of comments in which case it's usually better to have a collection of comments per document. Say a document per comment page. Makes for very snappy pagination as well. If you don't expect many comments per post you can just embed them.

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