MongoDB-模拟联接或子查询 [英] MongoDB - simulate join or subquery
问题描述
我正在尝试找出在Mongo中构造数据的最佳方法,以模拟SQL中的简单联接或子查询.
I'm trying to figure out the best way to structure my data in Mongo to simulate what would be a simple join or subquery in SQL.
说,我有一个经典的用户和帖子"示例,其中用户"在一个集合中,而帖子"在另一个集合中.我想查找城市为伦敦"的用户的所有帖子.
Say I have the classic Users and Posts example, with Users in one collection and Posts in another. I want to find all posts by users who's city is "london".
我已经简化了这个问题,在现实世界中,将帖子作为数组存储在用户文档中将无法正常工作,因为每个用户不断插入1000个帖子".
I've simplified things in this question, in my real world scenario storing Posts as an array in the User document won't work as I have 1,000's of "posts" per user constantly inserting.
Mongos $ in运算符可以在这里提供帮助吗? $ in可以处理10,000,000个条目的数组吗?
Can Mongos $in operator help here? Can $in handle an array of 10,000,000 entries?
推荐答案
老实说,如果您不能将帖子"放入用户",那么您有两个选择.
Honestly, if you can't fit "Posts" into "Users", then you have two options.
- 对帖子内部的一些用户数据进行规范化.然后,您可以只搜索一个集合.
- 执行两个查询. (一个找到用户,另一个找到帖子)
根据您的问题,您正在尝试做#2.
Based on your question, you're trying to do #2.
从理论上讲,您可以构建一个用户ID(或引用)列表,然后找到属于该数组的用户$in
的所有帖子.但是显然,这种方法是有限的.
Theoretically, you could build a list of User IDs (or refs) and then find all Posts belonging to a User $in
that array. But obviously that approach is limited.
$ in可以处理10,000,000个条目的数组吗?
Can $in handle an array of 10,000,000 entries?
如果您打算针对一组 10,000,000个用户中的所有用户查询"您的帖子,则您已经远远超出了查询"阶段.您说自己每个用户都有1000篇帖子,所以您所说的是查询有伦敦帖子的用户" 返回100Ms的记录.
Look, if you're planning to "query" your posts for all users in a set of 10,000,000 Users you are well past the stage of "query". You say yourself that each User has 1,000s of posts so you're talking about a query for "Users with Posts who live in London" returning 100Ms of records.
1亿条记录不是查询,而是数据集!
如果您担心破坏$in
命令,那么我强烈建议您使用 map/reduce . Mongo Map/Reduce将为您创建一个新集合.然后,您可以根据需要调整或汇总此数据集.
If you're worried about breaking the $in
command, then I highly suggest that you use map/reduce. The Mongo Map/Reduce will create a new collection for you. You can then trim down or summarize this dataset as you see fit.
这篇关于MongoDB-模拟联接或子查询的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!