开发者

Accessing data from collectionB within a map/reduce performed on collectionA

How can I access data from collectionB within the "map" part of a map/reduce being performed on collectionA?

If it helps, I have phrases stored in collectionA which I want to split each phrase into individual words within the map, and then grab specific values for each word from collectionB.

In code, I imagined it would look something like this…

map = function() {
    var key, value;
    var results = db["collectionB"].find({something_related_to_collectionA});

    results.forEach(function(result) {
        // Change the value
    });

    emit(key, value);
};
reduce = function(key, values) {
    // Perform the reduce
};
db["collectionA"]开发者_JS百科.mapReduce(map, reduce, {out: "collectionC"});


So, in theory this should work. In practice though, you don't want to do this.

This type of lookup opens up a whole can of worms. For example how does this work with sharding? Imagine that your javascript is on shard2 and it's trying to access data from shard1, how does that work? shard2 doesn't even know that shard1 exists.

If you look at your code, you're fundamentally trying to re-create a JOIN statement. One of the premises for MongoDB's scalability is that it avoids joins b/c joins don't scale horizontally.

This may mean some more de-normalization or some form of pre-processing or some other system change. Ideally if you need to perform map-reduces on a collection, you'll design that collection to contain all of the data needed to perform this operation.


That should work fine.

db is accessible from pretty much anywhere as far as I can tell. I've ran a (quite useless) test and it worked pretty much as I'd expected. What problems are you having?

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜