Class: Mongoid::Collection
- Includes:
- Mongoid::Collections::Mimic
- Defined in:
- lib/mongoid/collection.rb
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 = {}) ⇒ Object
Determines where to send the next read query.
-
#find(selector = {}, options = {}) ⇒ Object
Find documents from the database given a selector and options.
-
#find_one(selector = {}, options = {}) ⇒ Object
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 = {}) ⇒ Object
(also: #mapreduce)
Perform a map/reduce on the documents.
-
#master ⇒ Object
Return the object responsible for writes to the database.
-
#slaves ⇒ Object
Return the object responsible for reading documents from the database.
Methods included from Mongoid::Collections::Mimic
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.
Example:
Mongoid::Collection.new(masters, slaves, "test")
77 78 79 |
# File 'lib/mongoid/collection.rb', line 77 def initialize(klass, name) @klass, @name = klass, name end |
Instance Attribute Details
#counter ⇒ Object (readonly)
Returns the value of attribute counter.
11 12 13 |
# File 'lib/mongoid/collection.rb', line 11 def counter @counter end |
#name ⇒ Object (readonly)
Returns the value of attribute name.
11 12 13 |
# File 'lib/mongoid/collection.rb', line 11 def name @name end |
Instance Method Details
#directed(options = {}) ⇒ Object
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.
Example:
collection.directed
Return:
Either a Master
or Slaves
collection.
32 33 34 35 36 |
# File 'lib/mongoid/collection.rb', line 32 def directed( = {}) .delete(:cache) enslave = .delete(:enslave) || @klass.enslaved? enslave ? master_or_slaves : master end |
#find(selector = {}, options = {}) ⇒ Object
Find documents from the database given a selector and options.
Options:
selector: A Hash
selector that is the query. options: The options to pass to the db.
Example:
collection.find({ :test => "value" })
48 49 50 51 52 53 54 55 |
# File 'lib/mongoid/collection.rb', line 48 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 = {}) ⇒ Object
Find the first document from the database given a selector and options.
Options:
selector: A Hash
selector that is the query. options: The options to pass to the db.
Example:
collection.find_one({ :test => "value" })
67 68 69 |
# File 'lib/mongoid/collection.rb', line 67 def find_one(selector = {}, = {}) directed().find_one(selector, ) end |
#map_reduce(map, reduce, options = {}) ⇒ Object Also known as: mapreduce
Perform a map/reduce on the documents.
Options:
map: The map javascript funcdtion. reduce: The reduce javascript function.
87 88 89 |
# File 'lib/mongoid/collection.rb', line 87 def map_reduce(map, reduce, = {}) directed().map_reduce(map, reduce, ) end |
#master ⇒ Object
Return the object responsible for writes to the database. This will always return a collection associated with the Master DB.
Example:
collection.writer
99 100 101 |
# File 'lib/mongoid/collection.rb', line 99 def master @master ||= Collections::Master.new(Mongoid.master, @name) end |
#slaves ⇒ Object
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.
Example:
collection.reader
110 111 112 |
# File 'lib/mongoid/collection.rb', line 110 def slaves @slaves ||= Collections::Slaves.new(Mongoid.slaves, @name) end |