How do I get a list of MongoDB documents that are referenced inside another collection

我怕爱的太早我们不能终老 提交于 2019-12-13 00:35:06

问题


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.

The User collection:

/* 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",
}

The Business collection

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

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");

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 ] } });

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

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

Any experts here on this?


回答1:


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

  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.

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



来源:https://stackoverflow.com/questions/28610445/how-do-i-get-a-list-of-mongodb-documents-that-are-referenced-inside-another-coll

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!