No release in over 3 years
Low commit activity in last 3 years
This gem for change Mongoid id field as Integer like MySQL.
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
 Dependencies

Development

~> 1.17
>= 0
~> 13.0
~> 3.0

Runtime

~> 7.0.5
 Project Readme

This gem for change Mongoid id field as Integer like MySQL.

Idea from MongoDB document: How to Make an Auto Incrementing Field

Status

  • Ruby Gem
  • Gem Version

Installation

gem 'mongo_auto_increment', "0.1.0"

Configure

If you want use sequence cache to reduce MongoDB write, you can enable cache:

config/initializes/mongoid_auto_increment_id.rb

# MongoAutoIncrement.cache_store = ActiveSupport::Cache::MemoryStore.new
# First call will generate 200 ids and caching in cache_store
# Then the next 199 ids will return from cache_store
# Until 200 ids used, it will generate next 200 ids again.
MongoAutoIncrement.seq_cache_size = 200

NOTE: 1) mongo_auto_increment is very fast in default config, you may don't need enable that, if you project not need insert huge rows in a moment. 2) The ID generated will be 64-bit combination of Timestamp and above sequence so that it will be unique in cluster mode and index will also work. ex: Model.last and Model.first

USAGE

ruby > post = Post.new(:title => "Hello world")
 => #<Post _id: 1582902420, _type: nil, title: "Hello world", body: nil>
ruby > post.save
 => true
ruby > post.inspect
 => "#<Post _id: 1582902420, _type: nil, title: \"Hello world\", body: nil>"
ruby > Post.find("1582902420")
 => "#<Post _id: 1582902420, _type: nil, title: \"Hello world\", body: nil>"
ruby > Post.find(1582902420)
 => "#<Post _id: 1582902420, _type: nil, title: \"Hello world\", body: nil>"
ruby > Post.desc(:_id).all.to_a.collect { |row| row.id }
 => [1582902420, 1582886820, 1582886818, 1582886729, 1582886728, 1582886722, 1582886720, 1582886714, 1582886696]

Performance

This is a branchmark results run in MacBook Pro Retina.

with mongoid_auto_increment_id:

       user     system      total        real
Generate 1  0.000000   0.000000   0.000000 (  0.004301)
Post current: 1

Generate 100  0.070000   0.000000   0.070000 (  0.091638)
Post current: 101

Generate 10,000  7.300000   0.570000   7.870000 (  9.962469)
Post current: 10101

without:

       user     system      total        real
Generate 1  0.000000   0.000000   0.000000 (  0.002569)
Post current: 1

Generate 100  0.050000   0.000000   0.050000 (  0.052045)
Post current: 101

Generate 10,000  5.220000   0.170000   5.390000 (  5.389207)
Post current: 10101