ruby: All content tagged as ruby in NoSQL databases and polyglot persistence
☞ MONGOID, one of the most popular — one as in currently I can count at least 4: MongoMapper and MONGOID compared, MongoODM, and Mongomatic, a minimal mapper — MongoDB Ruby-based “Object-Relational Mapper”
, got a ☞ cheat sheet.
If you are a rubyist working with MongoDB print it out as a PDF and have it around .
- Shouldn’t these mappers be called something like object-document mappers or document-object mappers? (↩)
Earlier this summer, we kicked off a series on MongoDB where the goal was to write a simple todo application using native MongoDB drivers and three of our favorite scripting languages.
Hongli Lai (FooBarWidget@GitHub) worked on some improvements of the MongoDB Ruby driver:
The Ruby driver was quite inefficient with handling data. Strings (read from the network or passed by the user) were being unpacked into arrays all over the place and vice versa. We’ve modified the driver to work with strings instead of byte arrays as much as possible. Most notably:
ByteBufferhas been rewritten to use a binary string as underlying storage object instead of an array.
The Ruby 1.8 implementation of
BSON::OrderedHashwas inefficient: it uses a Set even though it’s not necessary. We removed the dependency on Set and greatly improved
OrderedHash’s 1.8 performance.
The end result is a driver that’s 274% faster on Ruby 1.8 and 204% faster on Ruby 1.9.
There have been only a few days since I’ve written that (maybe) there are already too may mapping tools for document databases. Then, we’ve heard about Mongomatic: a minimal Ruby mapper for MongoDB. And now it is MongoODM, yet another Ruby mapper for MongoDB that can be found on ☞ GitHub.
Basically, I’ve tried to create an ODM that uses the native query syntax of the Ruby driver, but that was:
- Fully compatible with Rails 3
- Use the Mongo ruby driver syntax over a new syntax (for queries, cursors, indexes management…)
- Allow lazy loading of collections and queries nesting (concatenation of ‘find’ calls) to emulate ActiveRecord 3
- No association methods (for now): Just declare your own methods on models to fetch associated items
- Give support for dirty objects, validations, etc. through ActiveModel 3
- Automanage type conversions and default values
- Keep it as simple as possible
I made sure to ask the author, Carlos Paramio, why a new mapping tool for MongoDB? and what is it different in MongoODM compared to those other 3 MongoDB mapping libraries?
Carlos Paramio: The main reason for me creating this new ODM is that I felt the Mongo Ruby driver and its syntax (which is pretty similar to the native MongoDB syntax) is much more proper and richer than the syntax similar to the popular ActiveRecord ORM for Ruby — which is particularly designed for relational databases. Even if MongoMapper and MongoId are awesome projects, they both share this particular ActiveRecord-like syntax, which sometimes seems a bit forced and it generates weird bugs and unexpected behavior.
I preferred to use the official syntax and instead of writing tons of new code to bring all the other interesting features that you would expect from an Object Mapper (as an extra to the database driver), I used some stable and widely used Ruby libraries to provide: Validations, type conversions, dirty objects, nesting conditions, Rails 3 compatibility, etc.
There is a similar ODM by Ben Myles, who shares this same opinion about how a MongoDB ODM should work. It’s called MongoMatic, and it’s really cool. I think we started to code our ODMs at approximately same time, because we both have published stable versions of them recently (I’m particularly using MongoODM for my own project). MongoMatic doesn’t support some features like dirty objects, nesting conditions or Rails 3 compatibility yet, but it seems to be interesting to follow.
I think some people was expecting a solution like this. Like Kyle Banker has said (and I quote) recently in response to the MongoODM announcement at the mongodb-user mailing list: “Awesome. I love the trend of moving toward a more native MongoDB query syntax. Great to see the surge in new Ruby ODMs, too.”
Now the Ruby world of MongoDB has 4 mapping libraries and it is only the time and users that will decide if all of them are needed.
I hope someone will update the MongoDB Ruby libraries comparison, currently including Mongoid and MongoMapper, to also include Mongomatic. For an outsider it’s difficult to say what’s unique about it.