TinyMongo

Simple MongoDB wrapper

Notice

This gem is not yet ready for production use.

Install

gem install tinymongo

Rails Setup (Rails 3)

To create TinyMongo config file (config/tinymongo.yml) and initializer file (config/initializers/tinymongo.rb), do the following:

rails generate tinymongo

Connecting To MongoDB directly (for non-Rails projects)

TinyMongo.configure({:host => 'localhost', :database => 'db_name'})
TinyMongo.connect

Example

class Person < TinyMongo::Model
  mongo_collection :people          # optional if using Rails
  mongo_key :name
  mongo_key :age, :default => 0
  mongo_key :children, :default => []

  def make_child
    child = Person.create(:name => 'Baby')
    push({:children => child})      # push child into children array
  end

  def grow_up
    inc({:age => 1})                # increments age by 1
  end

  def set_stuff(n,a,c)
    # don't forget to put self
    self.name = n
    self.age = a
    self.children = c
    save
  end
end

Person.drop                         # empty drop collection

p = Person.create(:name => 'John', :age => 20)  # create John
p.make_child                        # make Baby and set it as John's child
p.grow_up                           # increment age by 1 = 21

Person.find.each do |person|
  puts person.name                  # print Baby and John
end

puts Person.find_one(:name => 'John').age  # print 21

Person.create(:name => 'Jim')
Person.create(:name => 'Pam')

puts Person.find.to_a.map { |person| person.name } # print Baby; John; Jim; Pam

# Sort by name (ascending), Skip Baby and Jim, limit result by 1, print => John
puts Person.find.sort({:name => 1}).skip(2).limit(1).next_document.name

Copyright © 2010 Peter Jihoon Kim. See LICENSE for details.