Daniel Doubrovkine bio photo

Daniel Doubrovkine

aka dB., @awscloud, former CTO @artsy, +@vestris, NYC

Email Twitter LinkedIn Github Strava
Creative Commons License

mongoid-logo-small

We high five each other and claim “Rails Win!” when someone finds a gem that solves a really big problem in a few lines one line of code. In this post I’ll list a few interesting addons to the RoR Mongoid ORM that are slowly adding up to a feature-rich ecosystem of plug-and-play modules that work with Mongoid. This list should convince you that you should at least try MongoDB. You can get a ton of functionality “for free”.

  • Mongoid::Timestamps: adds a timestamp to a model. IMHO all useful models should have a created_at and a modified_at field – it’s just a matter of time till someone asks you when your widget was modified. We use this.
  • Mongoid::Versioning: stores multiple versions of the document. We’ve tried to use this, but ended up switching to Mongoid::History (below).
  • Mongoid::Paranoya: marks documents as deleted instead of actually deleting them.
  • Mongoid::Slug: adds a URL-like field to a model. For example, our public API uses this field instead of the BSON ID itself, making the data portable across systems.
  • Mongoid::History: creates an audit trail for all changes in a model. It’s possible to undo and redo changes as well. We integrate this with our authentication and tag the identity of the person who made the change. I’ve been pushing features into this gem recently, including basic support for tracking object destruction.
  • Mongoid::Tracking: another tracker for document changes, more suitable when you need to embed tracking logic such as increments.
  • Mongoid::Taggable: adds tags to documents and make documents searchable by all or some tags.
  • Mongoid::TaggableWithContext: adds tagging support with pre-aggregation.
  • Mongoid::Fulltext: an n-gram based full text search. We wrote this to power auto-complete and avoid having an external search engine system. It has been growing nicely with improved relevance math and features like indexing multiple models that can be queried at-once.
  • Mongoid::Search: keyword-based search.
  • Mongoid::Geo: geo-based searched.
  • Mongoid::Il8n: localizable fields.
  • Mongoid::Votable: vote models up/down.
  • Mongoid::Tree: model tree structures.
  • Mongoid::Orderable and ActsAsList::Mongoid: makes your models orderable.
  • Mongoid::Atomic: atomic updates.

This is all made possible by the fact that MongoDB documents don’t need a schema and that mongoid has a very simple callback mechanism that can invoke your function when an object is created, modified or destroyed. If you want a skeleton for writing an extension, look at the source code of Mongoid::Fulltext – yes, the whole thing is 150 lines of code.

What other useful mongoid gems do you know about?