在MongoDB中过滤嵌入式文档 [英] Filtering embedded documents in MongoDB

查看:65
本文介绍了在MongoDB中过滤嵌入式文档的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在掌握如何过滤MongoDB中的嵌入式文档时遇到了麻烦,并且开始认为我应该使用关系关联,但是在文档存储环境中感觉很错误.

I am having trouble grasping how to filter embedded documents in MongoDB, and am starting to think I should be using a relational association, but that feels wrong in the document-store context.

在一个典型的博客/评论系统中,我有一个blogs的集合,每个blog都有许多comments.注释将作为嵌入式文档存储在博客文档中.

Sticking with a typical blog/comment system, I have a collection of blogs, and each blog has many comments. The comments are stored as embedded documents inside the blog document.

过滤我的blogs集合非常简单,但是要过滤嵌入在每个blog中的我的comments,我必须将它们全部加载到内存中(全部检索到Ruby数组中),并且循环浏览每个评论,返回符合特定条件的评论.

It is very simple to filter my blogs collection, but in order to filter my comments embedded in each blog, I am having to load them all into memory (retrieve all into a Ruby array), and loop through each comment, returning ones that match a specific criteria.

我使用点表示法过滤嵌入式文档的工作失败了,并带回了所有子文档.

My efforts to filter embedded documents using dot notation is failing, and bringing back all sub documents.

是否有更好的方法让MongoDB为我过滤这些内容,还是我应该辞职加入关系协会? (从长远来看,拉回所有嵌入的文档并手动进行过滤将非常耗时)

Is there a better way of getting MongoDB to filter these for me, or should I resign myself to relational associations? (Pulling back all embedded documents and manually filtering is going to be too intensive in the long run)

推荐答案

目前无法按照您所描述的方式对嵌入式文档进行过滤.使用点符号可以使您在嵌入式文档上进行匹配,但是整个文档(包括父文档和所有文档)仍将返回.也可以选择要返回的字段,但这对您的情况也无济于事.

There's currently no way to filter on embedded docs in the way you're describing. Using the dot notation allows you to match on an embedded doc, but the entire document, parent and all, will still be returned. It's also possible to select which fields will be returned, but that doesn't really help your case, either.

我们有一个虚拟馆藏"案例,它将实现所需的功能;随时对其投票:

We have a "virtual collections" case, which would implement the desired functionality; feel free to vote on it:

http://jira.mongodb.org/browse/SERVER-142

同时,您可能应该将评论视为自己的收藏.通常,如果您需要自己处理给定的数据集,请使其成为一个集合.如果最好将它作为其他集合的一部分来考虑,则最好嵌入.

In the meantime, you should probably treat comments as their own collection. In general, if you need to work with a given data set on its own, make it a collection. If it's better conceived of as part of some other set, it's better to embed.

这篇关于在MongoDB中过滤嵌入式文档的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆