Mongoid Sequence allows you to specify fields to behave like a sequence number (exactly like the "id" column in conventional SQL flavors).
Mongoid::Sequence in your class and call
class Sequenced include Mongoid::Document include Mongoid::Sequence field :my_sequence, :type => Integer sequence :my_sequence end s1 = Sequenced.create s1.sequence #=> 1 s2 = Sequenced.create s2.sequence #=> 2 # and so on
It's also possible to make the
id field behave like this:
class Sequenced include Mongoid::Document include Mongoid::Sequence sequence :_id end s1 = Sequenced.create s1.id #=> 1 s2 = Sequenced.create s2.id #=> 2 # and so on
Mongoid::Sequence uses the atomic findAndModify command, so you shouldn't have to worry about the sequence's consistency.
Just add it to your projects'
Copyright © 2010 Gonçalo Silva, released under the MIT license