如何获取另一个集合中引用的MongoDB文档的列表 [英] How do I get a list of MongoDB documents that are referenced inside another collection

查看:139
本文介绍了如何获取另一个集合中引用的MongoDB文档的列表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图找到一种方法来获取另一个集合的子文档中引用的MongoDB文档的列表。

I am trying to find a way to get a list of MongoDB documents that are referenced in a subdocument in another collection.

我有一个包含用户文档的集合。在另一个集合中,我保留了一个企业列表。每个企业都有一个子文档,其中包含一个对用户的引用列表。

I have a collection with user documents. In another collection I keep a list of businesses. Every business has a subdocument containing a list of references to users.

用户集合:

/* user-1 */
{
    "_id" : ObjectId("54e5e78680c7e191218b49b0"),
    "username" : "jachim@example.com",
    "password" "$2y$13$21p6hx3sd200cko4o0w04u46jNv3tNl3qpVWVbnAyzZpDxsSVDDLS"
}
/* user-2 */
{
    "_id" : ObjectId("54e5e78480c7e191218b49ab"),
    "username" : "jachim@example.net",
    "password" : "$2y$13$727amk1a7fwo4sgw8kkkcuWi4vhj2zKvZZIEDWtDQLo6dUjb0YnYy",
}

业务集合

/* business-1 */
{
    "_id" : ObjectId("54e5e78880c7e191218b4c52"),
    "name" : "Stack Overflow",
    "users" : [ 
        {
            "$ref" : "User",
            "$id" : ObjectId("54e5e78680c7e191218b49b0"),
            "$db" : "test"
        }
    ]
}

我可以通过遵循business.users列表中的引用来从公司中吸引用户使用 db.Business.find({ users。$ id:ObjectId( 54e5e78480c7e191218b49ab)})从用户那里获取业务,但是 I无法创建查询来查找在某处某处引用的所有用户

I can get the user from a business by following the references in the business.users list, I can get the businesses from a user with the db.Business.find({"users.$id": ObjectId("54e5e78480c7e191218b49ab")}) query, but I cannot create a query to find all users that are referenced somewhere in a business.

我可以分两个步骤执行此客户端操作:

I can do this client side in two steps:

db.Business.distinct("users.$id");

这将返回用户ID列表。我可以在查询用户集合时使用以下列表:

Which will return a list of user ids. This list I can use in a query to the user collection:

db.User.find({ _id: { $in: [ LIST_OF_IDS ] } });

但这可能会导致非常大的查询(可能导致大于16MB的查询)。

But this could result in very big queries (potentially leading to queries larger than 16MB).

我认为MapReduce将是一个解决方案,但是我不确定在该使用什么字段。

I think MapReduce would be a solution for this, but I'm not quite sure what fields I should use there.

这里有专家吗?

推荐答案

经过更多研究和在MongoDB IRC频道上的聊天之后,有几种选择使其工作:

After some more research and a chat on the MongoDB IRC channel, there are several options to get this to work:


  1. 继续使用 $ in 查询。

  2. 跟踪双方的关系(要使双方的关系保持最新状态会更难一些,但确实可行)。

  3. 更改所有权关系的一面(在用户文档中跟踪业务),但这取决于查询的性质。

  1. Go with the $in query.
  2. Keep track of the relation on both sides (a little harder to keep the relations up to date on both sides, but it works).
  3. Change the owning side of the relation (keeping track of businesses in the user document), but this depends on the nature of your queries.

href = http://docs.mongodb.org/manual/applications/aggregation/ rel = nofollow>聚合框架不起作用,因为它无法查询多个collectio ns, MapReduce 也不会(出于相同的原因,尽管这是可能的)。

The Aggregation Framework would not work, because it cannot query multiple collections, nor will MapReduce (for the same reason, although it is possible).

这篇关于如何获取另一个集合中引用的MongoDB文档的列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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