如果ID是一个简单的数字,尝试:
class Account
include Mongoid::Document
include Mongoid::Timestamps
identity :type => Integer
end
account = Account.new :id => 1111222233334444
#=> #<Account _id: 1111222233334444, created_at: nil, updated_at: nil>
account.save
#=> true
account
#=> #<Account _id: 1111222233334444, created_at: 2010-11-26 00:48:27 UTC, updated_at: 2010-11-26 00:48:27 UTC>
Account.count
#=> 1
Account.first
#=> #<Account _id: 1111222233334444, created_at: 2010-11-26 00:48:27 UTC, updated_at: 2010-11-26 00:48:27 UTC>
如果你想在ID使用字母太,你可以做identity :type => String
代替。