The Meteor feature that will Transform your Mongo Collections

How to easily keep track of data relationships

David Woody
2 min readOct 9, 2014

I was working on a way to more easily keep track of data relationships.

I was looking at some great packages on atmosphere like collection helpers, collection hooks, minimongoid, etc. However, I stumbled upon this great documented feature built right into Meteor Collections.

It allows you to transform documents returned by findOne() or fetch().

@nwientge pointed out collection helpers uses the transform function under the hood.

So I was thinking, what if you could create a “relations” prototype to keep track of all relationships between Mongo collections, and at the same time be able to share that prototype (or “mapping”) between all Collections?

You could define all of your relationships in one file like relations.js.

And since every collection would share the same prototype, you could traverse through data more easily.

For example:

Note.findOne().related(‘personId’).related(‘notes’);

could return all of the notes written by a Person for the given Note. Let’s walk through what the above could do.

You can reference the code provided below to follow along with the step-by-step.

  1. Note.findOne() returns a note object which has passed through a transform function and has a few extra properties: related and relations.
  2. The related(‘personId’) function above looks for a property called personId on the Note object, and finds it.
  3. It then calls the relations.personId method and passes in the personId to return the related Person object.
  4. The returned Person object also has a related() method except this time notes is passed as the argument. It looks for a property called notes on the Person object, but does not find it. So instead it calls the relations.notes method and passes in it’s own _id to return all the notes.
https://gist.github.com/davidwoody/2cae06ed4dadd3ff7379

This is just one example of how to use the transform function. This really opens up complete flexibility. I feel like this is an under-explored feature (at least for me). How else might you use this feature? Do you see any drawbacks to doing something like this?

Feedback is welcomed.

Thanks for the feedback! Keep it coming. This has gone through a few revisions based on feedback of earlier versions. Is anything confusing to you? Does this even make sense at all?

--

--

David Woody

Partner at Differential. JavaScript Developer. Certified Public Accountant. Formal Education in Finance, Economics, and Accounting. Mini-writing: @davidjwoody