Class: Mongoid::Contextual::Mongo
- Inherits:
-
Object
- Object
- Mongoid::Contextual::Mongo
- Includes:
- Enumerable, Atomic, Aggregable::Mongo
- Defined in:
- lib/mongoid/contextual/mongo.rb
Constant Summary
Constants included from Atomic
Instance Attribute Summary collapse
-
#criteria ⇒ Object
readonly
Returns the value of attribute criteria.
- #criteria The criteria for the context.(Thecriteria) ⇒ Object readonly
-
#eager_loaded ⇒ Object
Returns the value of attribute eager_loaded.
- #eager_loaded Has the context been eager loaded?(Hasthecontextbeeneagerloaded?) ⇒ Object
-
#klass ⇒ Object
readonly
Returns the value of attribute klass.
- #klass The klass for the criteria.(Theklass) ⇒ Object readonly
-
#query ⇒ Object
readonly
Returns the value of attribute query.
- #query The Moped query.(TheMopedquery.) ⇒ Object readonly
Instance Method Summary collapse
-
#blank? ⇒ true, false
(also: #empty?)
Is the enumerable of matching documents empty?.
-
#cached? ⇒ true, false
Is the context cached?.
-
#count(document = nil, &block) ⇒ Integer
Get the number of documents matching the query.
-
#delete ⇒ nil
(also: #delete_all)
Delete all documents in the database that match the selector.
-
#destroy ⇒ nil
(also: #destroy_all)
Destroy all documents in the database that match the selector.
-
#distinct(field) ⇒ Array<Object>
Get the distinct values in the db for the provided field.
-
#each(&block) ⇒ Enumerator
Iterate over the context.
-
#exists? ⇒ true, false
Do any documents exist for the context.
-
#explain ⇒ Hash
Run an explain on the criteria.
-
#find_and_modify(update, options = {}) ⇒ Document
Execute the find and modify command, used for MongoDB’s $findAndModify.
-
#first ⇒ Document
(also: #one)
Get the first document in the database for the criteria’s selector.
-
#initialize(criteria) ⇒ Mongo
constructor
Create the new Mongo context.
-
#last ⇒ Document
Get the last document in the database for the criteria’s selector.
-
#length ⇒ Integer
(also: #size)
Get’s the number of documents matching the query selector.
-
#limit(value) ⇒ Mongo
Limits the number of documents that are returned from the database.
-
#map_reduce(map, reduce) ⇒ MapReduce
Initiate a map/reduce operation from the context.
-
#skip(value) ⇒ Mongo
Skips the provided number of documents.
-
#sort(values = nil, &block) ⇒ Mongo
Sorts the documents by the provided spec.
-
#update(attributes = nil) ⇒ nil, false
(also: #update_all)
Update all the matching documents atomically.
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.
212 213 214 215 216 217 |
# File 'lib/mongoid/contextual/mongo.rb', line 212 def initialize(criteria) @criteria, @klass, @cache = criteria, criteria.klass, criteria.[:cache] add_type_selection @query = klass.collection.find(criteria.selector) end |
Instance Attribute Details
#criteria ⇒ Object (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_loaded ⇒ Object
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 |
#klass ⇒ Object (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 |
#query ⇒ Object (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?
31 32 33 |
# File 'lib/mongoid/contextual/mongo.rb', line 31 def blank? count == 0 end |
#cached? ⇒ true, false
Is the context cached?
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.
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 |
#delete ⇒ nil Also known as: delete_all
Delete all documents in the database that match the selector.
80 81 82 83 84 |
# File 'lib/mongoid/contextual/mongo.rb', line 80 def delete query.count.tap do query.remove_all end end |
#destroy ⇒ nil Also known as: destroy_all
Destroy all documents in the database that match the selector.
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.
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.
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.
152 153 154 |
# File 'lib/mongoid/contextual/mongo.rb', line 152 def exists? count > 0 end |
#explain ⇒ Hash
Run an explain on the criteria.
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.
184 185 186 187 188 |
# File 'lib/mongoid/contextual/mongo.rb', line 184 def find_and_modify(update, = {}) if doc = FindAndModify.new(criteria, update, ).result Factory.from_db(klass, doc) end end |
#first ⇒ Document Also known as: one
Get the first document in the database for the criteria’s selector.
198 199 200 |
# File 'lib/mongoid/contextual/mongo.rb', line 198 def first with_eager_loading(query.first) end |
#last ⇒ Document
Get the last document in the database for the criteria’s selector.
227 228 229 230 |
# File 'lib/mongoid/contextual/mongo.rb', line 227 def last apply_inverse_sorting with_eager_loading(query.first) end |
#length ⇒ Integer Also known as: size
Get’s the number of documents matching the query selector.
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.
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.
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.
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.
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.
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 |