As you guys already know that MongoDB isn’t suitable for complex queries. There are couple of ways to do it like Map-Reduce. But they will turn the whole application slow. So we can’t do that as we use MongoDB and Node.JS for couple of facilities and speed is one of them. So the best solution is to do it programmatically. Let’s see how to do it.

Suppose we’ve two MongoDB collections like:

Stores

{
    "id": "id",
    "name": "store name",
    ...
}

Extras

{
    "id": "id",
    "store": "store id here",
    "address": "store address here",
    ...
}

So, now we wanna join the two tables to gather the total information about the store. Now let’s do our homework, let’s try to do this.

###Trial 1

// we want our result here
// var store = ...
stores.findOne(id, function gotStore(firstRes) {
	extras.findOne(firstRes.id, function gotExtra(secondRes) {
		// So we've all data here
	});
});

Now the problem is how to sync the process so that we can get the data and don’t get the NULL. So this system isn’t working as we are getting NULL at the end. Let’s see what we can do with it.

###Trial 2

As we are using Node.js 0.10 we can’t use generators. Let’s go deep in the callback hell.

function getStore(id, callback) {
    stores.findOne(id, function gotStore(firstRes) {
	    extras.findOne(firstRes.id, function gotExtra(secondRes) {
	    	// So we've all data here
	    	var result = firstRes.concat(secondRes)
	    	callback(result);
	    });
    });
}

So now we are getting the values asynchronously we can do our job like this:

getStore(100, function printStore(store) {
    console.log (store.name);
});

There is almost 4 callback functions. That’s Okay for this sample as this sample is short. Real projects are too big and there will be a mess of callback functions. This is called Callback hell.

There are couple of ways to deal with Callback hell. Read my article about solving Callback Hell in Node.js

Thanks. Good luck with your own work.