简体   繁体   中英

MongoDb - “join” or $in two collections

Suppose I have 2 collections:

> db.fellas.findOne()
{
    "_id" : 123
    "women" : [
        1,
        12,
        34
    ]
}

> db.women.findOne()
{ "_id" : 12, "busty" : 1 }

Some of the women belong to a certain fella, but some of them don't. In the case above, the woman does belong to the fella.

How do I find all women whose don't belong to any fella? I tried to do that using $unwind, but no luck. I mean, I'm unaware what I should do further.

Your ideas?

You can use a function like the following one:

db.women.find().forEach(function(w){
  if( !db.fellas.findOne({ women:{$in:[w._id]} }) )
    printjson(w); 
)}

There is another way, namely mapreduce.

You can't do it with the agregation framework because it involves two diferent collections. You must take each women id and find it in women array of fellas collections.

Sounds like the aggregation framework does not support joins. This can be done (I have done it) with Hadoop, Apache Pig and the MongoDb+Hadoop connector. Apache Pig provides a high-level language that makes it pretty easy to join multiple data sources. It translates into map-reduce jobs that run in a Hadoop cluster.

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