Giter Club home page Giter Club logo

mongo_sequence's Introduction

MongoSequence

MongoSequence provides light-weight, robust sequences in MongoDB. Such sequences are useful for auto-incrementing or counting. Compatible with any Mongo ODM.

MongoSequence creates named sequences in a "sequences" collection in your database and atomically increments and returns the counter on them using Mongo's findAndModify command. You won't have collisions—two processes trying to increment at the same time will always get different numbers.

Usage

Install with Bundler:

gem "mongo_sequence"

Install without Bundler:

gem install mongo_sequence --no-ri --no-rdoc

If you're not using MongoMapper or Mongoid, you'll have to tell MongoSequence what database to use:

MongoSequence.database = Mongo::Connection.new.db('my_app_development')

Now, increment some sequences:

MongoSequence[:global].next    # => 1
MongoSequence[:global].next    # => 2

# get the current value...
# no guarantees of course on how long it's valid
# usually you use the return value of #next
MongoSequence[:global].current # => 2

# can also reset sequences if you need to
MongoSequence[:global] = 100
MongoSequence[:global].next    # => 101

# sequences with different names are independent
MongoSequence[:bluejay].next   # => 1
MongoSequence[:bluejay].next   # => 2

Here's how the sequences look in Mongo:

MongoSequence.collection.find_one(:_id => 'bluejay')
# =>
# {
#   "_id"     => "bluejay",
#   "current" => 2
# }

Why?

Why would anyone need atomically incrementing sequences with unique return values? Well, the most common case is for auto-incrementing id's in Mongo. Here's a MongoMapper example:

class Peregrine
  include MongoMapper::Document
  key :_id, Integer, :default => lambda { nil }

  before_create do
    self.id ||= MongoSequence[:peregrine_id].next # for id's unique among Peregrines
    # or
    self.id ||= MongoSequence[:mongo_id].next     # for id's unique across the database
  end
end

Help make it better!

Need something added? Please open an issue! Or, even better, code it yourself and send a pull request:

# fork it on github, then clone:
git clone [email protected]:your_username/mongo_sequence.git
bundle install
rspec
# hack away
git push
# then make a pull request

This fork

All I've done here is merge the mongo2_fix branch into master so I can use it in my own projects.

Inspiration

A first implimentation was written over a year ago based on Chris Shiflett's Auto Increment with MongoDB blog post.

License

Authored by Brian Hempel. Public domain, no restrictions.

mongo_sequence's People

Contributors

andytinycat avatar brianhempel avatar

Watchers

 avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.