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.
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:
- It requires the corruption index for reference as part of the loan object (line 4)
- It defines a virtual object (line 107) – including which fields to join on
- It makes sure the virtual objects are populated for each find against the loan collection (line 114)
- 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):
- The handlebars #if, else, /if is going to give me nightmares.
- 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: