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
-
#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.
-
#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) ⇒ Collection
Initialize a new Mongoid::Collection, setting up the master, slave, and name attributes. Masters will be used for writes, slaves for reads.
61 62 63 |
# File 'lib/mongoid/collection.rb', line 61 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
#find(selector = {}, options = {}) ⇒ Cursor
Find documents from the database given a selector and options.
31 32 33 34 35 36 37 38 |
# File 'lib/mongoid/collection.rb', line 31 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.
49 50 51 |
# File 'lib/mongoid/collection.rb', line 49 def find_one(selector = {}, = {}) master().find_one(selector, ) end |
#insert(documents, options = {}) ⇒ Object
Inserts one or more documents in the collection.
77 78 79 80 81 82 83 84 |
# File 'lib/mongoid/collection.rb', line 77 def insert(documents, = {}) inserter = Thread.current[:mongoid_batch_insert] if inserter inserter.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.
96 97 98 |
# File 'lib/mongoid/collection.rb', line 96 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.
108 109 110 111 112 |
# File 'lib/mongoid/collection.rb', line 108 def master( = {}) .delete(:cache) db = Mongoid.databases[@klass.database] || Mongoid.master @master ||= Collections::Master.new(db, @name) end |
#update(selector, document, options = {}) ⇒ Object
Updates one or more documents in the collection.
128 129 130 131 132 133 134 135 |
# File 'lib/mongoid/collection.rb', line 128 def update(selector, document, = {}) updater = Thread.current[:mongoid_atomic_update] if updater updater.consume(selector, document, ) else master().update(selector, document, ) end end |