Combining mongoDB collections to show on one page

In my current project, I’m displaying a country’s corruption trend from from Transparency International alongside Kiva loans. Since both of these data sets have different refresh intervals and sources, I do a mongoImport to get them into the database.

Example records:

The data is pretty clean at the get-go with a logical join on the location.country field from loans with the Country field from the corruption index.

Doing this in nodeJS isn’t so simple though.

First, we have to define the models:

For the corruption index model, I have:

The loan model is a bit different in 4 ways:

  1. It requires the corruption index for reference as part of the loan object (line 4)
  2. It defines a virtual object (line 107) – including which fields to join on
  3. It makes sure the virtual objects are populated for each find against the loan collection (line 114)
  4. It allows the populated virtual data to be accessible to toObject & toJSON nodeJS calls (line 102/103).

Here’s the full code:

Next, verify your controller code

Now for the controller, there is no need to call populate or attempt to merge the collections together. That logic stays in the model which is where we want it.

I learned through trial & error that toObject in this view was needed so that the view can see the dynamic data. 

Finally, we have the view bring it all together

Some learnings out of this (and I’m still iterating on look/feel):

  1. The handlebars #if, else, /if is going to give me nightmares.
  2. Bootstrap rocks. Makes it easy to iterate on the the right presentation with highly descriptive examples.

 

All of that will get you something that looks like this:

Leave a Reply

Powered by WordPress.com.

Up ↑

%d bloggers like this: