Alan Coromano
Alan Coromano

Reputation: 26018

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?

Upvotes: 3

Views: 2285

Answers (3)

Marquez
Marquez

Reputation: 6039

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.

Upvotes: 0

Muatik
Muatik

Reputation: 4171

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.

Upvotes: 2

francadaval
francadaval

Reputation: 2481

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.

Upvotes: 0

Related Questions