简体   繁体   中英

MapReduce on two Collections using MongoDB and C#

I have two collections with the following details:

Comments

{"_id" : ObjectId("521588ccb5d44d23aca151a2"), "UserId" : "5215862eb5d44d23aca1519d", "Comment" : "hello" }
{"_id" : ObjectId("521588ccb5d44d23aca151a3"), "UserId" : "5215862eb5d44d23aca1519e", "Comment" : "this is cool" }

"Comment" : "hello" } {"_id" : ObjectId("521588ccb5d44d23aca151a4"), "UserId" : "5215862eb5d44d23aca1519e", "Comment" : "I like mongo" }

User

{ "_id" : ObjectId("5215862eb5d44d23aca1519d"), "Nickname" : "Jane"}
{ "_id" : ObjectId("5215862eb5d44d23aca1519e"), "Nickname" : "Jon"}

How would I achieve the following using MapReduce (and is it the best for the task at hand)? This collection could ideally also be very large. ie hundreds of thousands or millions.

{ "UserId" : "5215862eb5d44d23aca1519d", "Comment": "Hello", "Nickname" : "Jane"}
{ "UserId" : "5215862eb5d44d23aca1519e", "Comment": "this is cool", "Nickname" : "Jon"}
{ "UserId" : "5215862eb5d44d23aca1519e", "Comment": "I like Mongo", "Nickname" : "Jon"}

Bote: I am using MongoDB with the .NET C# drivers.

MapReduce is not a good tool for this task, because a MapReduce query is always performed on a single collection. You would have to perform a MapReduce on one collection, and in the mapping function read from the other. But the documentation explicitely warns that you shouldn't do this: "The reduce function should not access the database, even to perform read operations" .

MongoDB in general is not designed for performing JOIN-operations.

What you can do:

Solution A: Perform the JOIN on the application layer. First query the comment collection and then use the results to query the user collection in a second query.

Solution B: Store a copy of the authors nickname in the comment documents so you don't have to query the user collection (In document-oriented databases, redundancies aren't as evil as in relational databases).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM