read

Today I've released the mongo-sugar
gem, which wraps MongoDB collections into classes, that you may extend.

The reasons for using mongo-sugar are:

  • Define collections as models
  • No need of relationship features
  • Doing raw MongoDB queries, such as multiple atomic operations (ORMs doesn't do that)

Your collection definitions will look like this:

class MyCollection < Mongo::Sugar::Collection
  # Define store collection (optional)
  store_in :collection => "collection"

  # Define your custom methods, like this:
  def do_upsert(hash)
    @collection.update({
      key: hash[:key]
    }, {
      :$addToSet => {
        :value => hash[:value]
      },
      :$inc => {
        :value => 1
      }
    }, :upsert => true)
  end
end

If you need relationship features, you should just use a full-featured ORM, such
as Mongoid, or even use a relational database.

Blog Logo

Endel Dreyer

Full-stack developer. Loves Ruby and JavaScript.


Published

Image

bugfixer / @endel

About programming, tools and solving problems.

Back to Overview