Class: Mongoid::Collection
Overview
This class is the Mongoid wrapper to the Mongo Ruby driver’s collection object.
Instance Attribute Summary collapse
-
#counter ⇒ Object
readonly
Returns the value of attribute counter.
-
#name ⇒ Object
readonly
Returns the value of attribute name.
Instance Method Summary collapse
-
#directed(options = {}) ⇒ Master, Slaves
Determines where to send the next read query.
-
#find(selector = {}, options = {}) ⇒ Cursor
Find documents from the database given a selector and options.
-
#find_one(selector = {}, options = {}) ⇒ Document?
Find the first document from the database given a selector and options.
-
#initialize(klass, name) ⇒ Collection
constructor
Initialize a new Mongoid::Collection, setting up the master, slave, and name attributes.
-
#map_reduce(map, reduce, options = {}) ⇒ Cursor
(also: #mapreduce)
Perform a map/reduce on the documents.
-
#master ⇒ Master
Return the object responsible for writes to the database.
-
#slaves ⇒ Slaves
Return the object responsible for reading documents from the database.
-
#update(selector, document, options = {}) ⇒ Object
Updates one or more documents in the collection.
Constructor Details
#initialize(klass, name) ⇒ Collection
Initialize a new Mongoid::Collection, setting up the master, slave, and name attributes. Masters will be used for writes, slaves for reads.
82 83 84 |
# File 'lib/mongoid/collection.rb', line 82 def initialize(klass, name) @klass, @name = klass, name end |
Instance Attribute Details
#counter ⇒ Object (readonly)
Returns the value of attribute counter.
13 14 15 |
# File 'lib/mongoid/collection.rb', line 13 def counter @counter end |
#name ⇒ Object (readonly)
Returns the value of attribute name.
13 14 15 |
# File 'lib/mongoid/collection.rb', line 13 def name @name end |
Instance Method Details
#directed(options = {}) ⇒ Master, Slaves
Determines where to send the next read query. If the slaves are not defined then send to master. If the read counter is under the configured maximum then return the master. In any other case return the slaves.
37 38 39 40 41 |
# File 'lib/mongoid/collection.rb', line 37 def directed( = {}) .delete(:cache) enslave = .delete(:enslave) || @klass.enslaved? enslave ? master_or_slaves : master end |
#find(selector = {}, options = {}) ⇒ Cursor
Find documents from the database given a selector and options.
52 53 54 55 56 57 58 59 |
# File 'lib/mongoid/collection.rb', line 52 def find(selector = {}, = {}) cursor = Mongoid::Cursor.new(@klass, self, directed().find(selector, )) if block_given? yield cursor; cursor.close else cursor end end |
#find_one(selector = {}, options = {}) ⇒ Document?
Find the first document from the database given a selector and options.
70 71 72 |
# File 'lib/mongoid/collection.rb', line 70 def find_one(selector = {}, = {}) directed().find_one(selector, ) end |
#map_reduce(map, reduce, options = {}) ⇒ Cursor Also known as: mapreduce
Perform a map/reduce on the documents.
96 97 98 |
# File 'lib/mongoid/collection.rb', line 96 def map_reduce(map, reduce, = {}) directed().map_reduce(map, reduce, ) end |
#master ⇒ Master
Return the object responsible for writes to the database. This will always return a collection associated with the Master DB.
108 109 110 111 |
# File 'lib/mongoid/collection.rb', line 108 def master db = Mongoid.databases[@klass.database] || Mongoid.master @master ||= Collections::Master.new(db, @name) end |
#slaves ⇒ Slaves
Return the object responsible for reading documents from the database. This is usually the slave databases, but in their absence the master will handle the task.
121 122 123 124 |
# File 'lib/mongoid/collection.rb', line 121 def slaves slaves = Mongoid.databases["#{@klass.database}_slaves"] || Mongoid.slaves @slaves ||= Collections::Slaves.new(slaves, @name) end |
#update(selector, document, options = {}) ⇒ Object
Updates one or more documents in the collection.
140 141 142 143 144 145 146 147 |
# File 'lib/mongoid/collection.rb', line 140 def update(selector, document, = {}) updater = Thread.current[:mongoid_atomic_update] if updater updater.consume(selector, document, ) else master.update(selector, document, ) end end |