Skip to content Skip to sidebar Skip to footer

Finding Intersection Between Two Collections In Mongodb

I have two very large(30000+ documents) collections, one contains words extracted from a text file(collection name 'word') and one contains words from a dictionary(collection name

Solution 1:

Copy both collections into a single collection (include a discriminator field if necessary so you can tell what kind of document you have in each instance).

Run map-reduce on that collection

In Map, emit the word as the key and a value, say {instance:1, dict:0} or {instance:0, dict:1} depending on whether the document being mapped is an instance or a dictionary entry. (You could add more fields here into the values as necessary.)

In Reduce, accumulate the scores (as usual).

Now do a query looking for instance > 0 and dict > 0 and you have all of the words that are in both.

Solution 2:

let

 db.word.findOne() >{ word:'a_word', ... }

 db.dict.findOne() >{ word:'a_word', def:'def_of_a_word', ... }

find words in word col.

db.word.distinct('word')

check if a_word exists in dict col.

db.dict.count({word:'a_word'})  // 0=not exist

Post a Comment for "Finding Intersection Between Two Collections In Mongodb"