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.
-
#klass ⇒ Object
readonly
Returns the value of attribute klass.
-
#name ⇒ Object
readonly
Returns the value of attribute name.
Instance Method Summary collapse
-
#driver ⇒ Mongo::Collection
Get the unwrapped driver collection for this mongoid collection.
-
#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, options = {}) ⇒ Collection
constructor
Initialize a new Mongoid::Collection, setting up the master, slave, and name attributes.
-
#insert(documents, options = {}) ⇒ Object
Inserts one or more documents in the collection.
-
#map_reduce(map, reduce, options = {}) ⇒ Cursor
(also: #mapreduce)
Perform a map/reduce on the documents.
-
#master(options = {}) ⇒ Master
Return the object responsible for writes to the database.
-
#update(selector, document, options = {}) ⇒ Object
Updates one or more documents in the collection.
Constructor Details
#initialize(klass, name, options = {}) ⇒ Collection
Initialize a new Mongoid::Collection, setting up the master, slave, and name attributes. Masters will be used for writes, slaves for reads.
77 78 79 |
# File 'lib/mongoid/collection.rb', line 77 def initialize(klass, name, = {}) @klass, @name, @options = 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 |
#klass ⇒ Object (readonly)
Returns the value of attribute klass.
11 12 13 |
# File 'lib/mongoid/collection.rb', line 11 def klass @klass 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
#driver ⇒ Mongo::Collection
Get the unwrapped driver collection for this mongoid collection.
28 29 30 |
# File 'lib/mongoid/collection.rb', line 28 def driver master.collection end |
#find(selector = {}, options = {}) ⇒ Cursor
Find documents from the database given a selector and options.
41 42 43 44 45 46 47 48 |
# File 'lib/mongoid/collection.rb', line 41 def find(selector = {}, = {}) cursor = Mongoid::Cursor.new(klass, self, master().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.
59 60 61 |
# File 'lib/mongoid/collection.rb', line 59 def find_one(selector = {}, = {}) master().find_one(selector, ) end |
#insert(documents, options = {}) ⇒ Object
Inserts one or more documents in the collection.
93 94 95 96 97 98 99 100 |
# File 'lib/mongoid/collection.rb', line 93 def insert(documents, = {}) consumer = Threaded.insert if consumer consumer.consume(documents, ) else master().insert(documents, ) end end |
#map_reduce(map, reduce, options = {}) ⇒ Cursor Also known as: mapreduce
Perform a map/reduce on the documents.
112 113 114 |
# File 'lib/mongoid/collection.rb', line 112 def map_reduce(map, reduce, = {}) master().map_reduce(map, reduce, ) end |
#master(options = {}) ⇒ Master
Return the object responsible for writes to the database. This will always return a collection associated with the Master DB.
124 125 126 127 128 |
# File 'lib/mongoid/collection.rb', line 124 def master( = {}) .delete(:cache) db = Mongoid.databases[klass.database] || Mongoid.master @master ||= Collections::Master.new(db, @name, @options) end |
#update(selector, document, options = {}) ⇒ Object
Updates one or more documents in the collection.
144 145 146 147 148 149 150 151 |
# File 'lib/mongoid/collection.rb', line 144 def update(selector, document, = {}) updater = Threaded.update_consumer(klass) if updater updater.consume(selector, document, ) else master().update(selector, document, ) end end |