Tag: Ruby

Mongoid: Serialize to JSON with an string id attribute

A quick hint, on how to have an 'id' attribute in Mongoid document JSON:

When you serialize Mongoid objects to JSON, you won't get ID attribute, similar to ActiveRecord. Instead you'll have "_id" attribute containing "$oid" value:

{
  "_id": {"$oid":"52d3fdfd53656e5180020000"},
  "changed_at":"2014-02-15",
  "created_at":"2014-01-13T15:53:49.444+01:00",
  "updated_at":"2014-01-13T15:53:49.444+01:00"
}

If you would prefer something like this:

{
  "id": "52d3fdfd53656e5180020000",
  "_id": {"$oid":"52d3fdfd53656e5180020000"},
  "changed_at":"2014-02-15",
  "created_at":"2014-01-13T15:53:49.444+01:00",
  "updated_at":"2014-01-13T15:53:49.444+01:00"
}

Just put this into your initializers (for example in config/initializers/mongoid.rb):

module Mongoid
  module Document
    def as_json(options={})
      attrs = super(options)
      attrs["id"] = attrs["_id"].to_s
      attrs
    end
  end
end

Using multiple MongoDB databases instead of one – performance check

I'm starting to develop a new application. Can't say what it is, but it perfectly fits MongoDB Document Oriented Database approach. Everything is great. except of small detail - I don't want to store everything in one database. Of course I could use collections and embedded documents to organize whole nested structures and keep users stuff separated, although it would make source code much more complicated than it should be. Instead I've decided to use one MongoDB database per user. That way I can separate users data and I don't need to worry about scoping it out. There will be a gateway, that will authorize incoming requests to a proper database.

Gateway

Schema is pretty straightforward and the only thing that was bothering me was the multi-db switching performance. I've decided to make a simple benchmark that would test if there's a difference when using one or many databases. Results are promising. It seems, that there's only around 5% (5.3% exactly) performance loss when using many databases instead of one. 5% is a difference level that I can easily accept. To be honest I think, I will gain much more especially when I will have a lot of data. Lets say I have 100 customers with 100 000 000 records. With one database I would have to query all of it. With separate databases, I will have to query only 1% of it.

Below you can see performance difference when querying one vs many MongoDB databases.

Dbs I will definitely go with that approach and I will try to keep you posted.

Note: This is not a full-pro-extremely accurate long-time test - more like a proof of concept. Keep that in mind ;)

Copyright © 2025 Closer to Code

Theme by Anders NorenUp ↑