Class: Mongoid::Contextual::Mongo

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

Constant Summary

Constant Summary

Constants included from Atomic

Atomic::UPDATES

Instance Attribute Summary (collapse)

Instance Method Summary (collapse)

Methods included from Atomic

#add_atomic_pull, #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

Methods included from Aggregable::Mongo

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

Constructor Details

- (Mongo) initialize(criteria)

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



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

def initialize(criteria)
  @criteria, @klass = criteria, criteria.klass
  add_type_selection
  @query = klass.collection.find(criteria.selector)
  apply_options
end

Instance Attribute Details

- (Object) criteria (readonly)

Returns the value of attribute criteria



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

def criteria
  @criteria
end

- (Object) criteria The criteria for the context. (readonly)



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

attr_reader :criteria, :klass, :query

- (Object) eager_loaded

Returns the value of attribute eager_loaded



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

def eager_loaded
  @eager_loaded
end

- (Object) eager_loaded Has the context been eager loaded?



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

attr_accessor :eager_loaded

- (Object) klass (readonly)

Returns the value of attribute klass



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

def klass
  @klass
end

- (Object) klass The klass for the criteria. (readonly)



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

attr_reader :criteria, :klass, :query

- (Object) query (readonly)

Returns the value of attribute query



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

def query
  @query
end

- (Object) query The Moped query. (readonly)



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

attr_reader :criteria, :klass, :query

Instance Method Details

- (true, false) blank? 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

- (Integer) count(document = nil, &block)

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



54
55
56
57
58
# File 'lib/mongoid/contextual/mongo.rb', line 54

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

- (nil) delete 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



68
69
70
71
72
# File 'lib/mongoid/contextual/mongo.rb', line 68

def delete
  query.count.tap do
    query.remove_all
  end
end

- (nil) destroy 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



83
84
85
86
87
88
89
# File 'lib/mongoid/contextual/mongo.rb', line 83

def destroy
  destroyed = query.count
  each do |doc|
    doc.destroy
  end
  destroyed
end

- (Array<Object>) distinct(field)

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



102
103
104
# File 'lib/mongoid/contextual/mongo.rb', line 102

def distinct(field)
  query.distinct(field)
end

- (Enumerator) each

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



117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
# File 'lib/mongoid/contextual/mongo.rb', line 117

def each
  if block_given?
    selecting do
      if eager_loadable?
        docs = query.map{ |doc| Factory.from_db(klass, doc) }
        eager_load(docs)
        docs.each do |doc|
          yield doc
          increment_length
        end
        docs
      else
        query.each do |doc|
          yield Factory.from_db(klass, doc)
          increment_length
        end
        self
      end
    end
  else
    to_enum
  end
end

- (true, false) exists?

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



149
150
151
# File 'lib/mongoid/contextual/mongo.rb', line 149

def exists?
  count > 0
end

- (Hash) explain

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



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

def explain
  query.explain
end

- (Document) find_and_modify(update, options = {})

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.

Returns:

  • (Document)

    The result of the command.

Since:

  • 3.0.0



180
181
182
183
184
# File 'lib/mongoid/contextual/mongo.rb', line 180

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

- (Document) first 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



194
195
196
# File 'lib/mongoid/contextual/mongo.rb', line 194

def first
  with_eager_loading(query.first)
end

- (Document) last

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

Examples:

Get the last document.

context.last

Returns:

Since:

  • 3.0.0



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

def last
  apply_inverse_sorting
  with_eager_loading(query.first)
end

- (Integer) length 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



236
237
238
# File 'lib/mongoid/contextual/mongo.rb', line 236

def length
  @length ||= query.count
end

- (Mongo) limit(value)

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



251
252
253
# File 'lib/mongoid/contextual/mongo.rb', line 251

def limit(value)
  query.limit(value) and self
end

- (MapReduce) map_reduce(map, reduce)

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



266
267
268
# File 'lib/mongoid/contextual/mongo.rb', line 266

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

- (Mongo) skip(value)

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



280
281
282
# File 'lib/mongoid/contextual/mongo.rb', line 280

def skip(value)
  query.skip(value) and self
end

- (Mongo) sort(values)

Sorts the documents by the provided spec.

Examples:

Sort the documents.

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

Parameters:

  • values (Hash)

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

Returns:

  • (Mongo)

    The context.

Since:

  • 3.0.0



295
296
297
# File 'lib/mongoid/contextual/mongo.rb', line 295

def sort(values)
  query.sort(values) and self
end

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

Update all the matching documents atomically.

Examples:

Update all the matching documents.

context.update(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



309
310
311
312
# File 'lib/mongoid/contextual/mongo.rb', line 309

def update(attributes = nil)
  return false unless attributes
  query.update_all({ "$set" => attributes })
end