Class: Mongoid::Contextual::Mongo

Inherits:
Object
  • Object
show all
Extended by:
Forwardable
Includes:
Enumerable, Association::EagerLoadable, Atomic, Aggregable::Mongo, Queryable
Defined in:
build/mongoid-master/lib/mongoid/contextual/mongo.rb

Constant Summary collapse

OPTIONS =

Options constant.

[ :hint,
  :limit,
  :skip,
  :sort,
  :batch_size,
  :max_scan,
  :max_time_ms,
  :snapshot,
  :comment,
  :read,
  :cursor_type,
  :collation
].freeze

Constants included from Atomic

Atomic::UPDATES

Instance Attribute Summary collapse

Attributes included from Queryable

#collection, #collection The collection to query against., #criteria, #criteria The criteria for the context., #klass, #klass The klass for the criteria.

Instance Method Summary collapse

Methods included from Queryable

#blank?

Methods included from Association::EagerLoadable

#eager_load, #eager_loadable?, #preload

Methods included from Atomic

#add_atomic_pull, #add_atomic_unset, #atomic_array_add_to_sets, #atomic_array_pulls, #atomic_array_pushes, #atomic_attribute_name, #atomic_delete_modifier, #atomic_insert_modifier, #atomic_path, #atomic_paths, #atomic_position, #atomic_pulls, #atomic_pushes, #atomic_sets, #atomic_unsets, #atomic_updates, #delayed_atomic_pulls, #delayed_atomic_sets, #delayed_atomic_unsets, #flag_as_destroyed, #flagged_destroys, #process_flagged_destroys

Methods included from Aggregable::Mongo

#aggregates, #avg, #max, #min, #sum

Constructor Details

#initialize(criteria) ⇒ Mongo

Create the new Mongo context. This delegates operations to the underlying driver.

Examples:

Create the new context.

Mongo.new(criteria)

Parameters:



268
269
270
271
272
273
274
# File 'build/mongoid-master/lib/mongoid/contextual/mongo.rb', line 268

def initialize(criteria)
  @criteria, @klass = criteria, criteria.klass
  @collection = @klass.collection
  criteria.send(:merge_type_selection)
  @view = collection.find(criteria.selector, session: _session)
  apply_options
end

Instance Attribute Details

#viewObject (readonly)

Returns the value of attribute view.



38
39
40
# File 'build/mongoid-master/lib/mongoid/contextual/mongo.rb', line 38

def view
  @view
end

#view The Mongo collection view.(TheMongocollectionview.) ⇒ Object (readonly)



38
# File 'build/mongoid-master/lib/mongoid/contextual/mongo.rb', line 38

attr_reader :view

Instance Method Details

#count(options = {}, &block) ⇒ Integer

Get the number of documents matching the query.

Examples:

Get the number of matching documents.

context.count

Get the count of documents with the provided options.

context.count(limit: 1)

Get the count for where the provided block is true.

context.count do |doc|
  doc.likes > 1
end

Parameters:

  • options (Hash) (defaults to: {})

    The options, such as skip and limit to be factored into the count.

Returns:

  • (Integer)

    The number of matches.



57
58
59
60
# File 'build/mongoid-master/lib/mongoid/contextual/mongo.rb', line 57

def count(options = {}, &block)
  return super(&block) if block_given?
  view.count_documents(options)
end

#deletenil Also known as: delete_all

Delete all documents in the database that match the selector.

Examples:

Delete all the documents.

context.delete

Returns:

  • (nil)

    Nil.



87
88
89
# File 'build/mongoid-master/lib/mongoid/contextual/mongo.rb', line 87

def delete
  view.delete_many.deleted_count
end

#destroynil Also known as: destroy_all

Destroy all documents in the database that match the selector.

Examples:

Destroy all the documents.

context.destroy

Returns:

  • (nil)

    Nil.



98
99
100
101
102
103
104
# File 'build/mongoid-master/lib/mongoid/contextual/mongo.rb', line 98

def destroy
  each.inject(0) do |count, doc|
    doc.destroy
    count += 1 if acknowledged_write?
    count
  end
end

#distinct(field) ⇒ Array<Object>

Get the distinct values in the db for the provided field.

Examples:

Get the distinct values.

context.distinct(:name)

Parameters:

  • field (String | Symbol)

    The name of the field.

Returns:

  • (Array<Object>)

    The distinct values for the field.



115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
# File 'build/mongoid-master/lib/mongoid/contextual/mongo.rb', line 115

def distinct(field)
  name = if Mongoid.legacy_pluck_distinct
    klass.database_field_name(field)
  else
    klass.cleanse_localized_field_names(field)
  end

  view.distinct(name).map do |value|
    if Mongoid.legacy_pluck_distinct
      value.class.demongoize(value)
    else
      is_translation = "#{name}_translations" == field.to_s
      recursive_demongoize(name, value, is_translation)
    end
  end
end

#each(&block) ⇒ Enumerator

Iterate over the context. If provided a block, yield to a Mongoid document for each, otherwise return an enum.

Examples:

Iterate over the context.

context.each do |doc|
  puts doc.name
end

Returns:

  • (Enumerator)

    The enumerator.



141
142
143
144
145
146
147
148
149
150
# File 'build/mongoid-master/lib/mongoid/contextual/mongo.rb', line 141

def each(&block)
  if block_given?
    documents_for_iteration.each do |doc|
      yield_document(doc, &block)
    end
    self
  else
    to_enum
  end
end

#estimated_count(options = {}) ⇒ Integer

Get the estimated number of documents matching the query.

Unlike count, estimated_count does not take a block because it is not traditionally defined (with a block) on Enumarable like count is.

Examples:

Get the estimated number of matching documents.

context.estimated_count

Parameters:

  • options (Hash) (defaults to: {})

    The options, such as maxTimeMS to be factored into the count.

Returns:

  • (Integer)

    The number of matches.



74
75
76
77
78
79
# File 'build/mongoid-master/lib/mongoid/contextual/mongo.rb', line 74

def estimated_count(options = {})
  unless self.criteria.selector.empty?
    raise Mongoid::Errors::InvalidEstimatedCountCriteria.new(self.klass)
  end
  view.estimated_document_count(options)
end

#exists?true | false

Note:

We don't use count here since Mongo does not use counted b-tree indexes.

Do any documents exist for the context.

Examples:

Do any documents exist for the context.

context.exists?

Returns:

  • (true | false)

    If the count is more than zero.



161
162
163
# File 'build/mongoid-master/lib/mongoid/contextual/mongo.rb', line 161

def exists?
  !!(view.projection(_id: 1).limit(1).first)
end

#explainHash

Run an explain on the criteria.

Examples:

Explain the criteria.

Band.where(name: "Depeche Mode").explain

Returns:

  • (Hash)

    The explain result.



171
172
173
# File 'build/mongoid-master/lib/mongoid/contextual/mongo.rb', line 171

def explain
  view.explain
end

#fifthDocument | nil

Get the fifth document in the database for the criteria's selector.

Examples:

Get the fifth document.

context.fifth

Returns:

  • (Document | nil)

    The fifth document or nil if none is found.



706
707
708
# File 'build/mongoid-master/lib/mongoid/contextual/mongo.rb', line 706

def fifth
  retrieve_nth(4)
end

#fifth!Document

Get the fifth document in the database for the criteria's selector or raise an error if none is found.

Examples:

Get the fifth document.

context.fifth!

Returns:



720
721
722
# File 'build/mongoid-master/lib/mongoid/contextual/mongo.rb', line 720

def fifth!
  fifth || raise_document_not_found_error
end

#find_firstObject

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Return the first result without applying sort



231
232
233
234
235
236
# File 'build/mongoid-master/lib/mongoid/contextual/mongo.rb', line 231

def find_first
  if raw_doc = view.first
    doc = Factory.from_db(klass, raw_doc, criteria)
    eager_load([doc]).first
  end
end

#find_one_and_deleteDocument

Execute the find and modify command, used for MongoDB's $findAndModify. This deletes the found document.

Examples:

Execute the command.

context.find_one_and_delete

Returns:

  • (Document)

    The result of the command.



222
223
224
225
226
# File 'build/mongoid-master/lib/mongoid/contextual/mongo.rb', line 222

def find_one_and_delete
  if doc = view.find_one_and_delete
    Factory.from_db(klass, doc)
  end
end

#find_one_and_replace(replacement, options = {}) ⇒ Document

Execute the find and modify command, used for MongoDB's $findAndModify.

Examples:

Execute the command.

context.find_one_and_update({ likes: 1 })

Parameters:

  • replacement (Hash)

    The replacement.

  • options (Hash) (defaults to: {})

    The command options.

Options Hash (options):

  • :return_document (:before | :after)

    Return the updated document from before or after update.

  • :upsert (true | false)

    Create the document if it doesn't exist.

Returns:

  • (Document)

    The result of the command.



209
210
211
212
213
# File 'build/mongoid-master/lib/mongoid/contextual/mongo.rb', line 209

def find_one_and_replace(replacement, options = {})
  if doc = view.find_one_and_replace(replacement, options)
    Factory.from_db(klass, doc)
  end
end

#find_one_and_update(update, options = {}) ⇒ Document

Execute the find and modify command, used for MongoDB's $findAndModify.

Examples:

Execute the command.

context.find_one_and_update({ "$inc" => { likes: 1 }})

Parameters:

  • update (Hash)

    The updates.

  • options (Hash) (defaults to: {})

    The command options.

Options Hash (options):

  • :return_document (:before | :after)

    Return the updated document from before or after update.

  • :upsert (true | false)

    Create the document if it doesn't exist.

Returns:

  • (Document)

    The result of the command.



189
190
191
192
193
# File 'build/mongoid-master/lib/mongoid/contextual/mongo.rb', line 189

def find_one_and_update(update, options = {})
  if doc = view.find_one_and_update(update, options)
    Factory.from_db(klass, doc)
  end
end

#first(limit = nil) ⇒ Document | nil Also known as: one

Note:

Automatically adding a sort on _id when no other sort is defined on the criteria has the potential to cause bad performance issues. If you experience unexpected poor performance when using #first or #last and have no sort defined on the criteria, use #take instead. Be aware that #take won't guarantee order.

Get the first document in the database for the criteria's selector.

Examples:

Get the first document.

context.first

Parameters:

  • limit (Integer) (defaults to: nil)

    The number of documents to return.

Returns:

  • (Document | nil)

    The first document or nil if none is found.



557
558
559
560
561
562
563
# File 'build/mongoid-master/lib/mongoid/contextual/mongo.rb', line 557

def first(limit = nil)
  if limit.nil?
    retrieve_nth(0)
  else
    retrieve_nth_with_limit(0, limit)
  end
end

#first!Document

Note:

Automatically adding a sort on _id when no other sort is defined on the criteria has the potential to cause bad performance issues. If you experience unexpected poor performance when using #first! or #last! and have no sort defined on the criteria, use #take! instead. Be aware that #take! won't guarantee order.

Get the first document in the database for the criteria's selector or raise an error if none is found.

Examples:

Get the first document.

context.first!

Returns:



582
583
584
# File 'build/mongoid-master/lib/mongoid/contextual/mongo.rb', line 582

def first!
  first || raise_document_not_found_error
end

#fourthDocument | nil

Get the fourth document in the database for the criteria's selector.

Examples:

Get the fourth document.

context.fourth

Returns:

  • (Document | nil)

    The fourth document or nil if none is found.



682
683
684
# File 'build/mongoid-master/lib/mongoid/contextual/mongo.rb', line 682

def fourth
  retrieve_nth(3)
end

#fourth!Document

Get the fourth document in the database for the criteria's selector or raise an error if none is found.

Examples:

Get the fourth document.

context.fourth!

Returns:



696
697
698
# File 'build/mongoid-master/lib/mongoid/contextual/mongo.rb', line 696

def fourth!
  fourth || raise_document_not_found_error
end

#geo_near(coordinates) ⇒ GeoNear

Deprecated.

Execute a $geoNear command against the database.

Examples:

Find documents close to 10, 10.

context.geo_near([ 10, 10 ])

Find with spherical distance.

context.geo_near([ 10, 10 ]).spherical

Find with a max distance.

context.geo_near([ 10, 10 ]).max_distance(0.5)

Provide a distance multiplier.

context.geo_near([ 10, 10 ]).distance_multiplier(1133)

Parameters:

  • coordinates (Array<Float>)

    The coordinates.

Returns:

  • (GeoNear)

    The GeoNear command.



257
258
259
# File 'build/mongoid-master/lib/mongoid/contextual/mongo.rb', line 257

def geo_near(coordinates)
  GeoNear.new(collection, criteria, coordinates)
end

#last(limit = nil) ⇒ Document | nil

Note:

Automatically adding a sort on _id when no other sort is defined on the criteria has the potential to cause bad performance issues. If you experience unexpected poor performance when using #first or #last and have no sort defined on the criteria, use #take instead. Be aware that #take won't guarantee order.

Get the last document in the database for the criteria's selector.

Examples:

Get the last document.

context.last

Parameters:

  • limit (Integer) (defaults to: nil)

    The number of documents to return.

Returns:

  • (Document | nil)

    The last document or nil if none is found.



600
601
602
603
604
605
606
# File 'build/mongoid-master/lib/mongoid/contextual/mongo.rb', line 600

def last(limit = nil)
  if limit.nil?
    retrieve_nth_to_last(0)
  else
    retrieve_nth_to_last_with_limit(0, limit)
  end
end

#last!Document

Note:

Automatically adding a sort on _id when no other sort is defined on the criteria has the potential to cause bad performance issues. If you experience unexpected poor performance when using #first! or #last! and have no sort defined on the criteria, use #take! instead. Be aware that #take! won't guarantee order.

Get the last document in the database for the criteria's selector or raise an error if none is found.

Examples:

Get the last document.

context.last!

Returns:



624
625
626
# File 'build/mongoid-master/lib/mongoid/contextual/mongo.rb', line 624

def last!
  last || raise_document_not_found_error
end

#lengthInteger Also known as: size

Returns the number of documents in the database matching the query selector.

Examples:

Get the length.

context.length

Returns:

  • (Integer)

    The number of documents.



285
286
287
# File 'build/mongoid-master/lib/mongoid/contextual/mongo.rb', line 285

def length
  self.count
end

#limit(value) ⇒ Mongo

Limits the number of documents that are returned from the database.

Examples:

Limit the documents.

context.limit(20)

Parameters:

  • value (Integer)

    The number of documents to return.

Returns:

  • (Mongo)

    The context.



298
299
300
# File 'build/mongoid-master/lib/mongoid/contextual/mongo.rb', line 298

def limit(value)
  @view = view.limit(value) and self
end

#map_reduce(map, reduce) ⇒ MapReduce

Initiate a map/reduce operation from the context.

Examples:

Initiate a map/reduce.

context.map_reduce(map, reduce)

Parameters:

  • map (String)

    The map js function.

  • reduce (String)

    The reduce js function.

Returns:

  • (MapReduce)

    The map/reduce lazy wrapper.



311
312
313
# File 'build/mongoid-master/lib/mongoid/contextual/mongo.rb', line 311

def map_reduce(map, reduce)
  MapReduce.new(collection, criteria, map, reduce)
end

#pick(*fields) ⇒ Object | Array<Object>

Pick the single field values from the database.

Examples:

Pick a field.

context.pick(:_id)

Parameters:

  • *fields ([ String | Symbol ]...)

    Field(s) to pick.

Returns:

  • (Object | Array<Object>)

    The picked values.



368
369
370
# File 'build/mongoid-master/lib/mongoid/contextual/mongo.rb', line 368

def pick(*fields)
  limit(1).pluck(*fields).first
end

#pluck(*fields) ⇒ Array<Object> | Array<Array<Object>>

Pluck the field value(s) from the database. Returns one result for each document found in the database for the context. The results are normalized according to their Mongoid field types. Note that the results may include duplicates and nil values.

Examples:

Pluck a field.

context.pluck(:_id)

Parameters:

  • *fields ([ String | Symbol ]...)

    Field(s) to pluck, which may include nested fields using dot-notation.

Returns:

  • (Array<Object> | Array<Array<Object>>)

    The plucked values. If the *fields arg contains a single value, each result in the array will be a single value. Otherwise, each result in the array will be an array of values.



331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
# File 'build/mongoid-master/lib/mongoid/contextual/mongo.rb', line 331

def pluck(*fields)
  # Multiple fields can map to the same field name. For example, plucking
  # a field and its _translations field map to the same field in the database.
  # because of this, we need to keep track of the fields requested.
  normalized_field_names = []
  normalized_select = fields.inject({}) do |hash, f|
    db_fn = klass.database_field_name(f)
    normalized_field_names.push(db_fn)

    if Mongoid.legacy_pluck_distinct
      hash[db_fn] = true
    else
      hash[klass.cleanse_localized_field_names(f)] = true
    end
    hash
  end

  view.projection(normalized_select).reduce([]) do |plucked, doc|
    values = normalized_field_names.map do |n|
      if Mongoid.legacy_pluck_distinct
        n.include?('.') ? doc[n.partition('.')[0]] : doc[n]
      else
        extract_value(doc, n)
      end
    end
    plucked << (values.size == 1 ? values.first : values)
  end
end

#secondDocument | nil

Get the second document in the database for the criteria's selector.

Examples:

Get the second document.

context.second

Returns:

  • (Document | nil)

    The second document or nil if none is found.



634
635
636
# File 'build/mongoid-master/lib/mongoid/contextual/mongo.rb', line 634

def second
  retrieve_nth(1)
end

#second!Document

Get the second document in the database for the criteria's selector or raise an error if none is found.

Examples:

Get the second document.

context.second!

Returns:



648
649
650
# File 'build/mongoid-master/lib/mongoid/contextual/mongo.rb', line 648

def second!
  second || raise_document_not_found_error
end

#second_to_lastDocument | nil

Get the second to last document in the database for the criteria's selector.

is found.

Examples:

Get the second to last document.

context.second_to_last

Returns:

  • (Document | nil)

    The second to last document or nil if none



732
733
734
# File 'build/mongoid-master/lib/mongoid/contextual/mongo.rb', line 732

def second_to_last
  retrieve_nth_to_last(1)
end

#second_to_last!Document

Get the second to last document in the database for the criteria's selector or raise an error if none is found.

Examples:

Get the second to last document.

context.second_to_last!

Returns:

  • (Document)

    The second to last document.



746
747
748
# File 'build/mongoid-master/lib/mongoid/contextual/mongo.rb', line 746

def second_to_last!
  second_to_last || raise_document_not_found_error
end

#skip(value) ⇒ Mongo

Skips the provided number of documents.

Examples:

Skip the documents.

context.skip(20)

Parameters:

  • value (Integer)

    The number of documents to skip.

Returns:

  • (Mongo)

    The context.



487
488
489
# File 'build/mongoid-master/lib/mongoid/contextual/mongo.rb', line 487

def skip(value)
  @view = view.skip(value) and self
end

#sort(values = nil, &block) ⇒ Mongo

Sorts the documents by the provided spec.

Examples:

Sort the documents.

context.sort(name: -1, title: 1)

Parameters:

  • values (Hash) (defaults to: nil)

    The sorting values as field/direction(1/-1) pairs.

Returns:

  • (Mongo)

    The context.



500
501
502
503
504
505
506
507
508
509
# File 'build/mongoid-master/lib/mongoid/contextual/mongo.rb', line 500

def sort(values = nil, &block)
  if block_given?
    super(&block)
  else
    # update the criteria
    @criteria = criteria.order_by(values)
    apply_option(:sort)
    self
  end
end

#take(limit = nil) ⇒ Document | Array<Document>

Take the given number of documents from the database.

Examples:

Take 10 documents

context.take(10)

Parameters:

  • limit (Integer | nil) (defaults to: nil)

    The number of documents to return or nil.

Returns:

  • (Document | Array<Document>)

    The list of documents, or one document if no value was given.



381
382
383
384
385
386
387
388
389
# File 'build/mongoid-master/lib/mongoid/contextual/mongo.rb', line 381

def take(limit = nil)
  if limit
    limit(limit).to_a
  else
    # Do to_a first so that the Mongo#first method is not used and the
    # result is not sorted.
    limit(1).to_a.first
  end
end

#take!Document

Take one document from the database and raise an error if there are none.

Examples:

Take a document

context.take!

Returns:



400
401
402
403
404
405
406
407
408
# File 'build/mongoid-master/lib/mongoid/contextual/mongo.rb', line 400

def take!
  # Do to_a first so that the Mongo#first method is not used and the
  # result is not sorted.
  if fst = limit(1).to_a.first
    fst
  else
    raise Errors::DocumentNotFound.new(klass, nil, nil)
  end
end

#tally(field) ⇒ Hash

Get a hash of counts for the values of a single field. For example, if the following documents were in the database:

{ _id: 1, age: 21 }
{ _id: 2, age: 21 }
{ _id: 3, age: 22 }

Model.tally("age")

would yield the following result:

{ 21 => 2, 22 => 1 }

When tallying a field inside an array or embeds_many association:

{ _id: 1, array: [ { x: 1 }, { x: 2 } ] }
{ _id: 2, array: [ { x: 1 }, { x: 2 } ] }
{ _id: 3, array: [ { x: 1 }, { x: 3 } ] }

Model.tally("array.x")

The keys of the resulting hash are arrays:

{ [ 1, 2 ] => 2, [ 1, 3 ] => 1 }

Note that if tallying an element in an array of hashes, and the key doesn't exist in some of the hashes, tally will not include those nil keys in the resulting hash:

{ _id: 1, array: [ { x: 1 }, { x: 2 }, { y: 3 } ] }

Model.tally("array.x")
# => { [ 1, 2 ] => 1 }

Parameters:

  • field (String | Symbol)

    The field name.

Returns:

  • (Hash)

    The hash of counts.



447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
# File 'build/mongoid-master/lib/mongoid/contextual/mongo.rb', line 447

def tally(field)
  name = klass.cleanse_localized_field_names(field)

  fld = klass.traverse_association_tree(name)
  pipeline = [ { "$group" => { _id: "$#{name}", counts: { "$sum": 1 } } } ]
  pipeline.unshift("$match" => view.filter) unless view.filter.blank?

  collection.aggregate(pipeline).reduce({}) do |tallies, doc|
    is_translation = "#{name}_translations" == field.to_s
    val = doc["_id"]

    key = if val.is_a?(Array)
      val.map do |v|
        demongoize_with_field(fld, v, is_translation)
      end
    else
      demongoize_with_field(fld, val, is_translation)
    end

    # The only time where a key will already exist in the tallies hash
    # is when the values are stored differently in the database, but
    # demongoize to the same value. A good example of when this happens
    # is when using localized fields. While the server query won't group
    # together hashes that have other values in different languages, the
    # demongoized value is just the translation in the current locale,
    # which can be the same across multiple of those unequal hashes.
    tallies[key] ||= 0
    tallies[key] += doc["counts"]
    tallies
  end
end

#thirdDocument | nil

Get the third document in the database for the criteria's selector.

Examples:

Get the third document.

context.third

Returns:

  • (Document | nil)

    The third document or nil if none is found.



658
659
660
# File 'build/mongoid-master/lib/mongoid/contextual/mongo.rb', line 658

def third
  retrieve_nth(2)
end

#third!Document

Get the third document in the database for the criteria's selector or raise an error if none is found.

Examples:

Get the third document.

context.third!

Returns:



672
673
674
# File 'build/mongoid-master/lib/mongoid/contextual/mongo.rb', line 672

def third!
  third || raise_document_not_found_error
end

#third_to_lastDocument | nil

Get the third to last document in the database for the criteria's selector.

is found.

Examples:

Get the third to last document.

context.third_to_last

Returns:

  • (Document | nil)

    The third to last document or nil if none



758
759
760
# File 'build/mongoid-master/lib/mongoid/contextual/mongo.rb', line 758

def third_to_last
  retrieve_nth_to_last(2)
end

#third_to_last!Document

Get the third to last document in the database for the criteria's selector or raise an error if none is found.

Examples:

Get the third to last document.

context.third_to_last!

Returns:

  • (Document)

    The third to last document.



772
773
774
# File 'build/mongoid-master/lib/mongoid/contextual/mongo.rb', line 772

def third_to_last!
  third_to_last || raise_document_not_found_error
end

#update(attributes = nil, opts = {}) ⇒ nil | false

Update the first matching document atomically.

Examples:

Update the first matching document.

context.update({ "$set" => { name: "Smiths" }})

Parameters:

  • attributes (Hash) (defaults to: nil)

    The new attributes for the document.

  • opts (Hash) (defaults to: {})

    The update operation options.

Options Hash (opts):

  • :array_filters (Array)

    A set of filters specifying to which array elements an update should apply.

Returns:

  • (nil | false)

    False if no attributes were provided.



523
524
525
# File 'build/mongoid-master/lib/mongoid/contextual/mongo.rb', line 523

def update(attributes = nil, opts = {})
  update_documents(attributes, :update_one, opts)
end

#update_all(attributes = nil, opts = {}) ⇒ nil | false

Update all the matching documents atomically.

Examples:

Update all the matching documents.

context.update_all({ "$set" => { name: "Smiths" }})

Parameters:

  • attributes (Hash) (defaults to: nil)

    The new attributes for each document.

  • opts (Hash) (defaults to: {})

    The update operation options.

Options Hash (opts):

  • :array_filters (Array)

    A set of filters specifying to which array elements an update should apply.

Returns:

  • (nil | false)

    False if no attributes were provided.



539
540
541
# File 'build/mongoid-master/lib/mongoid/contextual/mongo.rb', line 539

def update_all(attributes = nil, opts = {})
  update_documents(attributes, :update_many, opts)
end