Class: Mongoid::Contextual::Mongo

Inherits:
Object
  • Object
show all
Includes:
Enumerable, Atomic, Aggregable::Mongo
Defined in:
lib/mongoid/contextual/mongo.rb

Constant Summary

Constants included from Atomic

Atomic::UPDATES

Instance Attribute Summary collapse

Instance Method Summary collapse

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_position, #atomic_pulls, #atomic_pushes, #atomic_selector, #atomic_sets, #atomic_unsets, #atomic_updates, #delayed_atomic_pulls, #delayed_atomic_sets, #delayed_atomic_unsets, #flag_as_destroyed

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 - in Mongoid’s case Moped.

Examples:

Create the new context.

Mongo.new(criteria)

Parameters:

Since:

  • 3.0.0



212
213
214
215
216
217
# File 'lib/mongoid/contextual/mongo.rb', line 212

def initialize(criteria)
  @criteria, @klass, @cache = criteria, criteria.klass, criteria.options[:cache]
  add_type_selection
  @query = klass.collection.find(criteria.selector)
  apply_options
end

Instance Attribute Details

#criteriaObject (readonly)

Returns the value of attribute criteria.



18
19
20
# File 'lib/mongoid/contextual/mongo.rb', line 18

def criteria
  @criteria
end

#criteria The criteria for the context.(Thecriteria) ⇒ Object (readonly)



18
# File 'lib/mongoid/contextual/mongo.rb', line 18

attr_reader :criteria, :klass, :query

#eager_loadedObject

Returns the value of attribute eager_loaded.



21
22
23
# File 'lib/mongoid/contextual/mongo.rb', line 21

def eager_loaded
  @eager_loaded
end

#eager_loaded Has the context been eager loaded?(Hasthecontextbeeneagerloaded?) ⇒ Object



21
# File 'lib/mongoid/contextual/mongo.rb', line 21

attr_accessor :eager_loaded

#klassObject (readonly)

Returns the value of attribute klass.



18
19
20
# File 'lib/mongoid/contextual/mongo.rb', line 18

def klass
  @klass
end

#klass The klass for the criteria.(Theklass) ⇒ Object (readonly)



18
# File 'lib/mongoid/contextual/mongo.rb', line 18

attr_reader :criteria, :klass, :query

#queryObject (readonly)

Returns the value of attribute query.



18
19
20
# File 'lib/mongoid/contextual/mongo.rb', line 18

def query
  @query
end

#query The Moped query.(TheMopedquery.) ⇒ Object (readonly)



18
# File 'lib/mongoid/contextual/mongo.rb', line 18

attr_reader :criteria, :klass, :query

Instance Method Details

#blank?true, false Also known as: empty?

Is the enumerable of matching documents empty?

Examples:

Is the context empty?

context.blank?

Returns:

  • (true, false)

    If the context is empty.

Since:

  • 3.0.0



31
32
33
# File 'lib/mongoid/contextual/mongo.rb', line 31

def blank?
  count == 0
end

#cached?true, false

Is the context cached?

Examples:

Is the context cached?

context.cached?

Returns:

  • (true, false)

    If the context is cached.

Since:

  • 3.0.0



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

def cached?
  !!@cache
end

#count(document = nil, &block) ⇒ Integer

Get the number of documents matching the query.

Examples:

Get the number of matching documents.

context.count

Get the count of documents matching the provided.

context.count(document)

Get the count for where the provided block is true.

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

Parameters:

  • document (Document) (defaults to: nil)

    A document ot match.

Returns:

  • (Integer)

    The number of matches.

Since:

  • 3.0.0



66
67
68
69
70
# File 'lib/mongoid/contextual/mongo.rb', line 66

def count(document = nil, &block)
  return super(&block) if block_given?
  return query.count unless document
  klass.collection.find(criteria.and(_id: document.id).selector).count
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.

Since:

  • 3.0.0



80
81
82
83
84
# File 'lib/mongoid/contextual/mongo.rb', line 80

def delete
  query.count.tap do
    query.remove_all
  end
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.

Since:

  • 3.0.0



95
96
97
98
99
100
101
# File 'lib/mongoid/contextual/mongo.rb', line 95

def destroy
  destroyed = query.count
  each do |doc|
    doc.destroy
  end
  destroyed
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.

Since:

  • 3.0.0



114
115
116
# File 'lib/mongoid/contextual/mongo.rb', line 114

def distinct(field)
  query.distinct(field)
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.

Since:

  • 3.0.0



129
130
131
132
133
134
135
136
137
138
139
140
141
142
# File 'lib/mongoid/contextual/mongo.rb', line 129

def each(&block)
  if block_given?
    reset_length
    selecting do
      documents_for_iteration.each do |doc|
        yield_and_increment(doc, &block)
      end
      @cache_loaded = true
      eager_loadable? ? docs : self
    end
  else
    to_enum
  end
end

#exists?true, false

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.

Since:

  • 3.0.0



152
153
154
# File 'lib/mongoid/contextual/mongo.rb', line 152

def exists?
  count > 0
end

#explainHash

Run an explain on the criteria.

Examples:

Explain the criteria.

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

Returns:

  • (Hash)

    The explain result.

Since:

  • 3.0.0



164
165
166
# File 'lib/mongoid/contextual/mongo.rb', line 164

def explain
  query.explain
end

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

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

Examples:

Execute the command.

context.find_and_modify({ "$inc" => { likes: 1 }}, new: true)

Parameters:

  • update (Hash)

    The updates.

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

    The command options.

Options Hash (options):

  • :new (true, false)

    Return the updated document.

  • :remove (true, false)

    Delete the first document.

  • :upsert (true, false)

    Create the document if it doesn’t exist.

Returns:

  • (Document)

    The result of the command.

Since:

  • 3.0.0



184
185
186
187
188
# File 'lib/mongoid/contextual/mongo.rb', line 184

def find_and_modify(update, options = {})
  if doc = FindAndModify.new(criteria, update, options).result
    Factory.from_db(klass, doc)
  end
end

#firstDocument Also known as: one

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

Examples:

Get the first document.

context.first

Returns:

Since:

  • 3.0.0



198
199
200
# File 'lib/mongoid/contextual/mongo.rb', line 198

def first
  with_eager_loading(query.first)
end

#lastDocument

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

Examples:

Get the last document.

context.last

Returns:

Since:

  • 3.0.0



227
228
229
230
# File 'lib/mongoid/contextual/mongo.rb', line 227

def last
  apply_inverse_sorting
  with_eager_loading(query.first)
end

#lengthInteger Also known as: size

Get’s the number of documents matching the query selector.

Examples:

Get the length.

context.length

Returns:

  • (Integer)

    The number of documents.

Since:

  • 3.0.0



240
241
242
# File 'lib/mongoid/contextual/mongo.rb', line 240

def length
  @length ||= query.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.

Since:

  • 3.0.0



255
256
257
# File 'lib/mongoid/contextual/mongo.rb', line 255

def limit(value)
  query.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.

Since:

  • 3.0.0



270
271
272
# File 'lib/mongoid/contextual/mongo.rb', line 270

def map_reduce(map, reduce)
  MapReduce.new(criteria, map, reduce)
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.

Since:

  • 3.0.0



284
285
286
# File 'lib/mongoid/contextual/mongo.rb', line 284

def skip(value)
  query.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.

Since:

  • 3.0.0



299
300
301
302
303
304
305
# File 'lib/mongoid/contextual/mongo.rb', line 299

def sort(values = nil, &block)
  if block_given?
    super(&block)
  else
    query.sort(values) and self
  end
end

#update(attributes = nil) ⇒ nil, false Also known as: update_all

Update all the matching documents atomically.

Examples:

Update all the matching documents.

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

Parameters:

  • attributes (Hash) (defaults to: nil)

    The new attributes for each document.

Returns:

  • (nil, false)

    False if no attributes were provided.

Since:

  • 3.0.0



317
318
319
320
# File 'lib/mongoid/contextual/mongo.rb', line 317

def update(attributes = nil)
  return false unless attributes
  query.update_all(attributes.__consolidate__)
end