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 collapse
- OPTIONS =
Options constant.
[ :hint, :limit, :skip, :sort, :batch_size, :max_scan, :snapshot, :comment, :read, :cursor_type, :collation ].freeze
Constants included from Atomic
Instance Attribute Summary collapse
-
#view ⇒ Object
readonly
Returns the value of attribute view.
- #view The Mongo collection view.(TheMongocollectionview.) ⇒ 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.
Instance Method Summary collapse
-
#cached? ⇒ true, false
Is the context cached?.
-
#count(options = {}, &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_first ⇒ Object
private
Return the first result without applying sort.
-
#find_one_and_delete ⇒ Document
Execute the find and modify command, used for MongoDB’s $findAndModify.
-
#find_one_and_replace(replacement, options = {}) ⇒ Document
Execute the find and modify command, used for MongoDB’s $findAndModify.
-
#find_one_and_update(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.
-
#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_loadable?, #preload
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.
324 325 326 327 328 329 330 |
# File 'lib/mongoid/contextual/mongo.rb', line 324 def initialize(criteria) @criteria, @klass, @cache = criteria, criteria.klass, criteria.[:cache] @collection = @klass.with(criteria. || {}).collection criteria.send(:merge_type_selection) @view = collection.find(criteria.selector) end |
Instance Attribute Details
#view ⇒ Object (readonly)
Returns the value of attribute view.
35 36 37 |
# File 'lib/mongoid/contextual/mongo.rb', line 35 def view @view end |
#view The Mongo collection view.(TheMongocollectionview.) ⇒ Object (readonly)
35 |
# File 'lib/mongoid/contextual/mongo.rb', line 35 attr_reader :view |
Instance Method Details
#cached? ⇒ true, false
Is the context cached?
45 46 47 |
# File 'lib/mongoid/contextual/mongo.rb', line 45 def cached? !!@cache end |
#count(options = {}, &block) ⇒ Integer
Get the number of documents matching the query.
68 69 70 71 |
# File 'lib/mongoid/contextual/mongo.rb', line 68 def count( = {}, &block) return super(&block) if block_given? try_cache(:count) { view.count() } end |
#delete ⇒ nil Also known as: delete_all
Delete all documents in the database that match the selector.
81 82 83 |
# File 'lib/mongoid/contextual/mongo.rb', line 81 def delete view.delete_many.deleted_count end |
#destroy ⇒ nil Also known as: destroy_all
Destroy all documents in the database that match the selector.
94 95 96 97 98 99 |
# File 'lib/mongoid/contextual/mongo.rb', line 94 def destroy each.inject(0) do |count, doc| doc.destroy count += 1 end end |
#distinct(field) ⇒ Array<Object>
Get the distinct values in the db for the provided field.
112 113 114 |
# File 'lib/mongoid/contextual/mongo.rb', line 112 def distinct(field) view.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.
127 128 129 130 131 132 133 134 135 136 137 |
# File 'lib/mongoid/contextual/mongo.rb', line 127 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.
151 152 153 154 155 156 157 158 |
# File 'lib/mongoid/contextual/mongo.rb', line 151 def exists? return !documents.empty? if cached? && cache_loaded? return @count > 0 if instance_variable_defined?(:@count) try_cache(:exists) do !!(view.projection(_id: 1).limit(1).first) end end |
#explain ⇒ Hash
Run an explain on the criteria.
168 169 170 |
# File 'lib/mongoid/contextual/mongo.rb', line 168 def explain view.explain end |
#find_first ⇒ Object
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
Return the first result without applying sort
261 262 263 264 265 266 267 |
# File 'lib/mongoid/contextual/mongo.rb', line 261 def find_first return documents.first if cached? && cache_loaded? if raw_doc = view.first doc = Factory.from_db(klass, raw_doc, criteria.[:fields]) eager_load([doc]).first end end |
#find_one_and_delete ⇒ Document
Execute the find and modify command, used for MongoDB’s $findAndModify. This deletes the found document.
225 226 227 228 229 |
# File 'lib/mongoid/contextual/mongo.rb', line 225 def find_one_and_delete if doc = view.find_one_and_delete Factory.from_db(klass, doc) end end |
#find_one_and_replace(replacement, options = {}) ⇒ Document
Execute the find and modify command, used for MongoDB’s $findAndModify.
210 211 212 213 214 |
# File 'lib/mongoid/contextual/mongo.rb', line 210 def find_one_and_replace(replacement, = {}) if doc = view.find_one_and_replace(replacement, ) Factory.from_db(klass, doc) end end |
#find_one_and_update(update, options = {}) ⇒ Document
Execute the find and modify command, used for MongoDB’s $findAndModify.
188 189 190 191 192 |
# File 'lib/mongoid/contextual/mongo.rb', line 188 def find_one_and_update(update, = {}) if doc = view.find_one_and_update(update, ) Factory.from_db(klass, doc) end end |
#first ⇒ Document Also known as: one
Mongoid previously added an _id sort when no sort parameters were provided explicitly by the user. This caused bad performance issues and was not expected, so #first/#last will no longer guarantee order if no sorting parameters are provided. For order guarantees - a sort must be explicitly provided.
Get the first document in the database for the criteria’s selector.
245 246 247 248 249 250 251 252 253 |
# File 'lib/mongoid/contextual/mongo.rb', line 245 def first return documents.first if cached? && cache_loaded? try_cache(:first) do if raw_doc = view.limit(-1).first doc = Factory.from_db(klass, raw_doc, criteria.[:fields]) eager_load([doc]).first end end end |
#geo_near(coordinates) ⇒ GeoNear
Execute a $geoNear command against the database.
288 289 290 |
# File 'lib/mongoid/contextual/mongo.rb', line 288 def geo_near(coordinates) GeoNear.new(collection, criteria, coordinates) end |
#last ⇒ Document
Mongoid previously added an _id sort when no sort parameters were provided explicitly by the user. This caused bad performance issues and was not expected, so #first/#last will no longer guarantee order if no sorting parameters are provided. For order guarantees - a sort must be explicitly provided.
Get the last document in the database for the criteria’s selector.
348 349 350 351 352 353 354 355 356 357 |
# File 'lib/mongoid/contextual/mongo.rb', line 348 def last try_cache(:last) do with_inverse_sorting do if raw_doc = view.limit(-1).first doc = Factory.from_db(klass, raw_doc, criteria.[:fields]) eager_load([doc]).first end end end end |
#length ⇒ Integer Also known as: size
Get’s the number of documents matching the query selector.
367 368 369 |
# File 'lib/mongoid/contextual/mongo.rb', line 367 def length @length ||= self.count end |
#limit(value) ⇒ Mongo
Limits the number of documents that are returned from the database.
382 383 384 |
# File 'lib/mongoid/contextual/mongo.rb', line 382 def limit(value) @view = view.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.
307 308 309 310 311 312 313 |
# File 'lib/mongoid/contextual/mongo.rb', line 307 def map(field = nil, &block) if block_given? super(&block) else criteria.pluck(field) end end |
#map_reduce(map, reduce) ⇒ MapReduce
Initiate a map/reduce operation from the context.
397 398 399 |
# File 'lib/mongoid/contextual/mongo.rb', line 397 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.
415 416 417 418 419 420 421 422 423 424 425 426 427 |
# File 'lib/mongoid/contextual/mongo.rb', line 415 def pluck(*fields) normalized_select = fields.inject({}) do |hash, f| hash[klass.database_field_name(f)] = 1 hash end view.projection(normalized_select).reduce([]) do |plucked, doc| values = normalized_select.keys.map do |n| n =~ /\./ ? doc[n.partition('.')[0]] : doc[n] end plucked << (values.size == 1 ? values.first : values) end end |
#skip(value) ⇒ Mongo
Skips the provided number of documents.
439 440 441 |
# File 'lib/mongoid/contextual/mongo.rb', line 439 def skip(value) @view = view.skip(value) and self end |
#sort(values = nil, &block) ⇒ Mongo
Sorts the documents by the provided spec.
454 455 456 457 458 459 460 461 462 463 |
# File 'lib/mongoid/contextual/mongo.rb', line 454 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 |
#update(attributes = nil) ⇒ nil, false
Update the first matching document atomically.
475 476 477 |
# File 'lib/mongoid/contextual/mongo.rb', line 475 def update(attributes = nil) update_documents(attributes) end |
#update_all(attributes = nil) ⇒ nil, false
Update all the matching documents atomically.
489 490 491 |
# File 'lib/mongoid/contextual/mongo.rb', line 489 def update_all(attributes = nil) update_documents(attributes, :update_many) end |