Module: Mongoid::Contextual::Atomic

Defined in:
lib/mongoid/contextual/atomic.rb

Instance Method Summary collapse

Instance Method Details

#add_to_set(adds) ⇒ nil

Execute an atomic $addToSet on the matching documents.

Examples:

Add the value to the set.

context.add_to_set(members: "Dave", genres: "Electro")

Parameters:

  • adds (Hash)

    The operations.

Returns:

  • (nil)

    Nil.

Since:

  • 3.0.0



16
17
18
# File 'lib/mongoid/contextual/atomic.rb', line 16

def add_to_set(adds)
  view.update_many("$addToSet" => collect_operations(adds))
end

#bit(bits) ⇒ nil

Perform an atomic $bit operation on the matching documents.

Examples:

Perform the bitwise op.

context.bit(likes: { and: 14, or: 4 })

Parameters:

  • bits (Hash)

    The operations.

Returns:

  • (nil)

    Nil.

Since:

  • 3.0.0



30
31
32
# File 'lib/mongoid/contextual/atomic.rb', line 30

def bit(bits)
  view.update_many("$bit" => collect_operations(bits))
end

#inc(incs) ⇒ nil

Perform an atomic $inc operation on the matching documents.

Examples:

Perform the atomic increment.

context.inc(likes: 10)

Parameters:

  • incs (Hash)

    The operations.

Returns:

  • (nil)

    Nil.

Since:

  • 3.0.0



44
45
46
# File 'lib/mongoid/contextual/atomic.rb', line 44

def inc(incs)
  view.update_many("$inc" => collect_operations(incs))
end

#pop(pops) ⇒ nil

Perform an atomic $pop operation on the matching documents.

Examples:

Pop the first value on the matches.

context.pop(members: -1)

Pop the last value on the matches.

context.pop(members: 1)

Parameters:

  • pops (Hash)

    The operations.

Returns:

  • (nil)

    Nil.

Since:

  • 3.0.0



61
62
63
# File 'lib/mongoid/contextual/atomic.rb', line 61

def pop(pops)
  view.update_many("$pop" => collect_operations(pops))
end

#pull(pulls) ⇒ nil

Note:

Expression pulling is not yet supported.

Perform an atomic $pull operation on the matching documents.

Examples:

Pull the value from the matches.

context.pull(members: "Dave")

Parameters:

  • pulls (Hash)

    The operations.

Returns:

  • (nil)

    Nil.

Since:

  • 3.0.0



77
78
79
# File 'lib/mongoid/contextual/atomic.rb', line 77

def pull(pulls)
  view.update_many("$pull" => collect_operations(pulls))
end

#pull_all(pulls) ⇒ nil

Perform an atomic $pullAll operation on the matching documents.

Examples:

Pull all the matching values from the matches.

context.pull_all(:members, [ "Alan", "Vince" ])

Parameters:

  • pulls (Hash)

    The operations.

Returns:

  • (nil)

    Nil.

Since:

  • 3.0.0



91
92
93
# File 'lib/mongoid/contextual/atomic.rb', line 91

def pull_all(pulls)
  view.update_many("$pullAll" => collect_operations(pulls))
end

#push(pushes) ⇒ nil

Perform an atomic $push operation on the matching documents.

Examples:

Push the value to the matching docs.

context.push(members: "Alan")

Parameters:

  • pushes (Hash)

    The operations.

Returns:

  • (nil)

    Nil.

Since:

  • 3.0.0



105
106
107
# File 'lib/mongoid/contextual/atomic.rb', line 105

def push(pushes)
  view.update_many("$push" => collect_operations(pushes))
end

#push_all(pushes) ⇒ nil

Perform an atomic $pushAll operation on the matching documents.

Examples:

Push the values to the matching docs.

context.push(members: [ "Alan", "Fletch" ])

Parameters:

  • pushes (Hash)

    The operations.

Returns:

  • (nil)

    Nil.

Since:

  • 3.0.0



119
120
121
# File 'lib/mongoid/contextual/atomic.rb', line 119

def push_all(pushes)
  view.update_many("$pushAll" => collect_operations(pushes))
end

#rename(renames) ⇒ nil

Perform an atomic $rename of fields on the matching documents.

Examples:

Rename the fields on the matching documents.

context.rename(members: :artists)

Parameters:

  • renames (Hash)

    The operations.

Returns:

  • (nil)

    Nil.

Since:

  • 3.0.0



133
134
135
136
137
138
139
# File 'lib/mongoid/contextual/atomic.rb', line 133

def rename(renames)
  operations = renames.inject({}) do |ops, (old_name, new_name)|
    ops[old_name] = new_name.to_s
    ops
  end
  view.update_many("$rename" => collect_operations(operations))
end

#set(sets) ⇒ nil

Perform an atomic $set of fields on the matching documents.

Examples:

Set the field value on the matches.

context.set(name: "Depeche Mode")

Parameters:

  • sets (Hash)

    The operations.

Returns:

  • (nil)

    Nil.

Since:

  • 3.0.0



151
152
153
# File 'lib/mongoid/contextual/atomic.rb', line 151

def set(sets)
  view.update_many("$set" => collect_operations(sets))
end

#unset(*args) ⇒ nil

Perform an atomic $unset of a field on the matching documents.

Examples:

Unset the field on the matches.

context.unset(:name)

Parameters:

  • fields (String, Symbol, Array)

    The name of the fields.

Returns:

  • (nil)

    Nil.

Since:

  • 3.0.0



165
166
167
168
# File 'lib/mongoid/contextual/atomic.rb', line 165

def unset(*args)
  fields = args.__find_args__.collect { |f| [database_field_name(f), true] }
  view.update_many("$unset" => Hash[fields])
end