Skip to content

Commit

Permalink
Merge pull request #12 from junwchina/master
Browse files Browse the repository at this point in the history
Works on replicate set mongodb well
  • Loading branch information
huacnlee committed Jun 4, 2014
2 parents 5d24129 + 04e292b commit eb5b71c
Showing 1 changed file with 12 additions and 5 deletions.
17 changes: 12 additions & 5 deletions lib/mongoid_auto_increment_id.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,18 @@ module Mongoid
class Identity
# Generate auto increment id
def self.generate_id(document)
o = Mongoid::Sessions.default.command({:findAndModify => "mongoid.auto_increment_ids",
:query => { :_id => document.collection_name },
:update => { "$inc" => { :c => 1 } },
:upsert => true,
:new => true })
database_name = Mongoid::Sessions.default.send(:current_database).name

o = nil
Mongoid::Sessions.default.cluster.with_primary do |node|
o = node.command(database_name,
{"findAndModify" => "mongoid.auto_increment_ids",
:query => { :_id => document.collection_name },
:update => { "$inc" => { :c => 1 }},
:upsert => true,
:new => true }, {})
end

o["value"]["c"].to_i
end
end
Expand Down

0 comments on commit eb5b71c

Please sign in to comment.