Q:

How can I merge/join then sort with mongodb

I have an entries collection and a domain collection like the following
entries:
{
    {
        entry_id:1,
        title:"Title 1",
        domain_id:1
    },
    {
        entry_id:2,
        title:"Title 2",
        domain_id:1
    },
    {
        entry_id:3,
        title:"Title 3",
        domain_id:2
    }
}

domains:
{
    {
        domain_id:1,
        domain:"Google",
        rank:1
    },
    {
        domain_id:2,
        domain:"Yahoo",
        rank:2
    }
}

I am looking to gather all the information together and sort by the rank of the domain. Coming from a MySQL background I would normally join the entries and domains on the domain_id and sort by the rank field. One of my co-workers suggested looking into map/reduce, but after doing some reading I'm unsure how to apply that here.

A:

As your co-worker said, Map/Reduce.
MongoDB is a document-based database. Probably a better pattern to follow in your scheme is to put entries inside the domains, see how much DRY it is:
{
    {
        domain_id:1,
        domain:"Google",
        rank:1,
        entries: [
          { 
            _id: dsfdsailfub3i4b23i4b234234,
            title: "the title"
          },
          { 
            _id: dsfdsailfub3i4b23i4b234234,
            title: "the title"
          }
        ]
    },
    {
        domain_id:2,
        domain:"Yahoo",
        rank:2
    }
}

That's the easy part. you can now easily query a certain domain. Now, if you want to know how many entries each domain has you map each entry to return 1, and you reduce all those ones to find out their sum.
Here are several great answers to what exactly is Map/Reduce. Once you wrap your head around the idea it will be easy to implement it in Mongo.

