Class: Mongoid::Contextual::Mongo
- Inherits:
-
Object
- Object
- Mongoid::Contextual::Mongo
- Includes:
- Enumerable, Atomic, Aggregable::Mongo, Queryable, Relations::Eager
- Defined in:
- lib/mongoid/contextual/mongo.rb
Constant Summary
Constants included from Atomic
Instance Attribute Summary collapse
-
#query ⇒ Object
readonly
Returns the value of attribute query.
- #query The Moped query.(TheMopedquery.) ⇒ Object readonly
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.
Attributes included from Relations::Eager
Instance Method Summary collapse
-
#cached? ⇒ true, false
Is the context cached?.
-
#count(document = false, &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.
-
#geo_near(coordinates) ⇒ GeoNear
Execute a $geoNear command against the database.
-
#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(field = nil, &block) ⇒ Array
Invoke the block for each element of Contextual.
-
#map_reduce(map, reduce) ⇒ MapReduce
Initiate a map/reduce operation from the context.
-
#pluck(*fields) ⇒ Array<Object, Array>
Pluck the single field values from the database.
-
#skip(value) ⇒ Mongo
Skips the provided number of documents.
-
#sort(values = nil, &block) ⇒ Mongo
Sorts the documents by the provided spec.
-
#text_search(query) ⇒ TextSearch
Execute a text command against the database.
-
#update(attributes = nil) ⇒ nil, false
Update the first matching document atomically.
-
#update_all(attributes = nil) ⇒ nil, false
Update all the matching documents atomically.
Methods included from Queryable
Methods included from Relations::Eager
#eager_load, #eager_load_one, #eager_loadable?, #preload, #with_eager_loading
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 - in Mongoid’s case Moped.
261 262 263 264 265 266 267 |
# File 'lib/mongoid/contextual/mongo.rb', line 261 def initialize(criteria) @criteria, @klass, @cache = criteria, criteria.klass, criteria.[:cache] @collection = @klass.with(criteria. || {}).collection criteria.send(:merge_type_selection) @query = collection.find(criteria.selector) end |
Instance Attribute Details
#query ⇒ Object (readonly)
Returns the value of attribute query.
21 22 23 |
# File 'lib/mongoid/contextual/mongo.rb', line 21 def query @query end |
#query The Moped query.(TheMopedquery.) ⇒ Object (readonly)
21 |
# File 'lib/mongoid/contextual/mongo.rb', line 21 attr_reader :query |
Instance Method Details
#cached? ⇒ true, false
Is the context cached?
31 32 33 |
# File 'lib/mongoid/contextual/mongo.rb', line 31 def cached? !!@cache end |
#count(document = false, &block) ⇒ Integer
Get the number of documents matching the query.
54 55 56 57 58 59 60 61 |
# File 'lib/mongoid/contextual/mongo.rb', line 54 def count(document = false, &block) return super(&block) if block_given? if document.is_a?(Document) return collection.find(criteria.and(_id: document.id).selector).count end return query.count(document) if document try_cache(:count) { query.count } end |
#delete ⇒ nil Also known as: delete_all
Delete all documents in the database that match the selector.
71 72 73 74 75 |
# File 'lib/mongoid/contextual/mongo.rb', line 71 def delete self.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.
86 87 88 89 90 91 92 |
# File 'lib/mongoid/contextual/mongo.rb', line 86 def destroy destroyed = self.count each do |doc| doc.destroy end destroyed end |
#distinct(field) ⇒ Array<Object>
Get the distinct values in the db for the provided field.
105 106 107 |
# File 'lib/mongoid/contextual/mongo.rb', line 105 def distinct(field) query.distinct(klass.database_field_name(field)) end |
#each(&block) ⇒ Enumerator
Iterate over the context. If provided a block, yield to a Mongoid document for each, otherwise return an enum.
120 121 122 123 124 125 126 127 128 129 130 |
# File 'lib/mongoid/contextual/mongo.rb', line 120 def each(&block) if block_given? documents_for_iteration.each do |doc| yield_document(doc, &block) end @cache_loaded = true self else to_enum end end |
#exists? ⇒ true, false
We don’t use count here since Mongo does not use counted b-tree indexes, unless a count is already cached then that is used to determine the value.
Do any documents exist for the context.
144 145 146 147 148 149 150 151 |
# File 'lib/mongoid/contextual/mongo.rb', line 144 def exists? return !documents.empty? if cached? && cache_loaded? return @count > 0 if instance_variable_defined?(:@count) try_cache(:exists) do !!(query.dup.select(_id: 1).limit(1).first) end end |
#explain ⇒ Hash
Run an explain on the criteria.
161 162 163 |
# File 'lib/mongoid/contextual/mongo.rb', line 161 def explain query.explain end |
#find_and_modify(update, options = {}) ⇒ Document
Execute the find and modify command, used for MongoDB’s $findAndModify.
181 182 183 184 185 |
# File 'lib/mongoid/contextual/mongo.rb', line 181 def find_and_modify(update, = {}) if doc = FindAndModify.new(collection, 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.
195 196 197 198 199 200 201 202 |
# File 'lib/mongoid/contextual/mongo.rb', line 195 def first return documents.first if cached? && cache_loaded? try_cache(:first) do with_sorting do with_eager_loading(query.first) end end end |
#geo_near(coordinates) ⇒ GeoNear
Execute a $geoNear command against the database.
224 225 226 |
# File 'lib/mongoid/contextual/mongo.rb', line 224 def geo_near(coordinates) GeoNear.new(collection, criteria, coordinates) end |
#last ⇒ Document
Get the last document in the database for the criteria’s selector.
279 280 281 282 283 284 285 |
# File 'lib/mongoid/contextual/mongo.rb', line 279 def last try_cache(:last) do with_inverse_sorting do with_eager_loading(query.first) end end end |
#length ⇒ Integer Also known as: size
Get’s the number of documents matching the query selector.
295 296 297 |
# File 'lib/mongoid/contextual/mongo.rb', line 295 def length @length ||= self.count end |
#limit(value) ⇒ Mongo
Limits the number of documents that are returned from the database.
310 311 312 |
# File 'lib/mongoid/contextual/mongo.rb', line 310 def limit(value) query.limit(value) and self end |
#map(field = nil, &block) ⇒ Array
Invoke the block for each element of Contextual. Create a new array containing the values returned by the block.
If the symbol field name is passed instead of the block, additional optimizations would be used.
243 244 245 246 247 248 249 250 |
# File 'lib/mongoid/contextual/mongo.rb', line 243 def map(field = nil, &block) if block_given? super(&block) else field = field.to_sym criteria.only(field).map(&field.to_proc) end end |
#map_reduce(map, reduce) ⇒ MapReduce
Initiate a map/reduce operation from the context.
325 326 327 |
# File 'lib/mongoid/contextual/mongo.rb', line 325 def map_reduce(map, reduce) MapReduce.new(collection, criteria, map, reduce) end |
#pluck(*fields) ⇒ Array<Object, Array>
This method will return the raw db values - it performs no custom serialization.
Pluck the single field values from the database. Will return duplicates if they exist and only works for top level fields.
343 344 345 346 347 348 349 350 351 352 353 354 355 356 |
# File 'lib/mongoid/contextual/mongo.rb', line 343 def pluck(*fields) normalized_select = fields.inject({}) do |hash, f| hash[klass.database_field_name(f)] = 1 hash end query.dup.select(normalized_select).map do |doc| if normalized_select.size == 1 doc[normalized_select.keys.first] else normalized_select.keys.map { |n| doc[n] }.compact end end.compact end |
#skip(value) ⇒ Mongo
Skips the provided number of documents.
368 369 370 |
# File 'lib/mongoid/contextual/mongo.rb', line 368 def skip(value) query.skip(value) and self end |
#sort(values = nil, &block) ⇒ Mongo
Sorts the documents by the provided spec.
383 384 385 386 387 388 389 390 391 392 |
# File 'lib/mongoid/contextual/mongo.rb', line 383 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 |
#text_search(query) ⇒ TextSearch
Execute a text command against the database.
404 405 406 |
# File 'lib/mongoid/contextual/mongo.rb', line 404 def text_search(query) TextSearch.new(collection, criteria, query) end |
#update(attributes = nil) ⇒ nil, false
Update the first matching document atomically.
418 419 420 |
# File 'lib/mongoid/contextual/mongo.rb', line 418 def update(attributes = nil) update_documents(attributes) end |
#update_all(attributes = nil) ⇒ nil, false
Update all the matching documents atomically.
432 433 434 |
# File 'lib/mongoid/contextual/mongo.rb', line 432 def update_all(attributes = nil) update_documents(attributes, :update_all) end |