【发布时间】:2013-11-21 22:00:58
【问题描述】:
我在 MongoDB 中有 3 个集合,它们的架构无法更改。一些查询需要访问 3 个集合。
我知道我需要多个查询来执行此操作,但我不确定执行此操作的最有效方法是什么。下面的例子被简化了:
我的数据包含一个“用户”集合,它充当其他两个集合的逻辑父级。另外两个集合是“DVD”和“CD”。一个用户可以拥有多张 CD 或 DVD
User Document
id : "jim",
location : "sweden"
CD Document
name : "White Album",
owner : "jim"
DVD Document
name : "Fargo",
owner : "jim"
现在,我目前采取的方法如下。如果我想取回瑞典用户的所有 CD 和 DVD。
第 1 步
Get all users in Sweden and return a cursor
第 2 步
Iterate through the each user in the cursor and perform a lookup on both the DVD and CD collections to see if the users id matches the owner field
第 3 步
If it does add the user to an array to be returned
这种方法需要 2 个额外的查询,对我来说似乎效率很低。有没有更有效的方法来做到这一点?
【问题讨论】:
-
为什么 CD 和 DVD 不在同一个集合中?
-
@Phillip 这只是一个简化的例子
标签: mongodb mongodb-query