0

I have one collection, called "games" whose documents store the ids of the owners of games.

{
    "owner" : "88878b6c25c167d"
}
{
    "owner" : "88878b6c25c167d"
}
{
    "owner" : "af565f77f73469b"
}

Then I have another collection called "users".

{
    "id" : "af565f77f73469b"
}
{
    "id" : "a881335e1d4cf17"
}
{
    "id" : "aa3ce3f7767c46b"
}
{
    "id" : "d19e52c0bd78bcb"
}
{
    "id" : "88878b6c25c167d"
}

So the first query I do retrieves the owners of all the games and stores those values in an array.['88878b6c25c167d', '88878b6c25c167d', 'af565f77f73469b'] The second query I want to perform should retrieve the documents of the users with the corresponding IDs. Previously I had used this query:

db.users.find({'id':
    {'$in': [
        '88878b6c25c167d',
        '88878b6c25c167d',
        'af565f77f73469b'
    ]}})

Here's the problem with this: It does not return duplicate documents if a user is listed twice, as above. Instead I just get one. This breaks my application. How can I make sure that each owner returns a document?

4

1 回答 1

1

MongoDB 工作得非常好——它找到了所有id-s包含在数组中的用户。

不知道您的需求的更广泛背景(也许告诉我们您想要实现的目标 - 不是什么问题?),但如果您想在 games和类似的users东西之间建立联系可能是合适的:

  1. 检索游戏集合后;只需创建一个辅助哈希映射(普通 JS 对象),对于给定owner id的将返回其游戏数组。
  2. 检索有关所有者用户的信息。
  3. 如果您想知道,哪些游戏属于特定user的,只需将她id从 1 传递到数据结构。并获取包含游戏的数组。

是你要找的吗?您需要 1. 方面的帮助吗?

于 2013-11-14T10:35:03.943 回答