Class: Mongoid::Contextual::Mongo
- Inherits:
-
Object
- Object
- Mongoid::Contextual::Mongo
- Includes:
- Enumerable, Atomic, Aggregable::Mongo
- Defined in:
- lib/mongoid/contextual/mongo.rb
Constant Summary
Constant Summary
Constants included from Atomic
Instance Attribute Summary (collapse)
-
- (Object) criteria
readonly
Returns the value of attribute criteria.
- - (Object) criteria The criteria for the context. readonly
-
- (Object) eager_loaded
Returns the value of attribute eager_loaded.
- - (Object) eager_loaded Has the context been eager loaded?
-
- (Object) klass
readonly
Returns the value of attribute klass.
- - (Object) klass The klass for the criteria. readonly
-
- (Object) query
readonly
Returns the value of attribute query.
- - (Object) query The Moped query. readonly
Instance Method Summary (collapse)
-
- (true, false) blank?
(also: #empty?)
Is the enumerable of matching documents empty?.
-
- (Integer) count(document = nil, &block)
Get the number of documents matching the query.
-
- (nil) delete
(also: #delete_all)
Delete all documents in the database that match the selector.
-
- (nil) destroy
(also: #destroy_all)
Destroy all documents in the database that match the selector.
-
- (Array<Object>) distinct(field)
Get the distinct values in the db for the provided field.
-
- (Enumerator) each
Iterate over the context.
-
- (true, false) exists?
Do any documents exist for the context.
-
- (Hash) explain
Run an explain on the criteria.
-
- (Document) find_and_modify(update, options = {})
Execute the find and modify command, used for MongoDB's $findAndModify.
-
- (Document) first
(also: #one)
Get the first document in the database for the criteria's selector.
-
- (Mongo) initialize(criteria)
constructor
Create the new Mongo context.
-
- (Document) last
Get the last document in the database for the criteria's selector.
-
- (Integer) length
(also: #size)
Get's the number of documents matching the query selector.
-
- (Mongo) limit(value)
Limits the number of documents that are returned from the database.
-
- (MapReduce) map_reduce(map, reduce)
Initiate a map/reduce operation from the context.
-
- (Mongo) skip(value)
Skips the provided number of documents.
-
- (Mongo) sort(values)
Sorts the documents by the provided spec.
-
- (nil, false) update(attributes = nil)
(also: #update_all)
Update all the matching documents atomically.
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.
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) 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?
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.
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.
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.
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.
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.
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.
149 150 151 |
# File 'lib/mongoid/contextual/mongo.rb', line 149 def exists? count > 0 end |
- (Hash) explain
Run an explain on the criteria.
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.
180 181 182 183 184 |
# File 'lib/mongoid/contextual/mongo.rb', line 180 def find_and_modify(update, = {}) if doc = FindAndModify.new(criteria, update, ).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.
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.
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.
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.
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.
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.
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.
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.
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 |