问题
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:
- Go with the
$in
query. - Keep track of the relation on both sides (a little harder to keep the relations up to date on both sides, but it works).
- 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