模块:Mongoid::Persistable::Incrementable

扩展方式:
ActiveSupport::Concern
包含在:
Mongoid::Persistable
定义于:
lib/mongoid/persistable/incrementable.rb

Overview

定义 $inc 操作的行为。

实例方法摘要折叠

实例方法详细信息

# inc (increments) ⇒文档

将提供的字段按相应的值递增。 值可以是正数或负数,如果该字段不存在值,则将其设置为提供的值。

例子:

递增字段。

document.inc(score: 10, place: 1, lives: -10)

参数:

  • 增量 (哈希)

    字段/inc 增量对。

返回:



21
22
23
24
25
26
27
28
29
30
31
32
33
# File 'lib/mongoid/persistable/incrementable.rb', line 21

def inc(增量)
  prepare_atomic_operation do |运维|
    process_atomic_operations(增量) do |字段, |
      增量 = .is_a?(BigDecimal) ? .to_f : 
      Current = 属性[字段]
      new_value = (Current || 0) + 增量
      process_attribute 字段, new_value if Executing_atomically?
      属性[字段] = new_value
      运维[atomic_attribute_name(字段)] = 增量
    end
    { " $inc " => 运维 } 除非 运维.空?
  end
end