Class: MongoDoc::Collection
Instance Attribute Summary collapse
-
#_collection ⇒ Object
Returns the value of attribute _collection.
Class Method Summary collapse
Instance Method Summary collapse
- #find(query = {}, options = {}) ⇒ Object
- #find_one(spec_or_object_id = nil, options = {}) ⇒ Object
-
#initialize(name) ⇒ Collection
constructor
A new instance of Collection.
- #insert(doc_or_docs, options = {}) ⇒ Object (also: #<<)
- #save(doc, options = {}) ⇒ Object
- #update(spec, doc, options = {}) ⇒ Object
Constructor Details
#initialize(name) ⇒ Collection
Returns a new instance of Collection.
8 9 10 |
# File 'lib/mongodoc/collection.rb', line 8 def initialize(name) self._collection = self.class.mongo_collection(name) end |
Instance Attribute Details
#_collection ⇒ Object
Returns the value of attribute _collection.
5 6 7 |
# File 'lib/mongodoc/collection.rb', line 5 def _collection @_collection end |
Class Method Details
Instance Method Details
#find(query = {}, options = {}) ⇒ Object
12 13 14 15 16 17 18 19 20 |
# File 'lib/mongodoc/collection.rb', line 12 def find(query = {}, = {}) cursor = MongoDoc::Cursor.new(_collection.find(query, )) if block_given? yield cursor cursor.close else cursor end end |
#find_one(spec_or_object_id = nil, options = {}) ⇒ Object
22 23 24 |
# File 'lib/mongodoc/collection.rb', line 22 def find_one(spec_or_object_id = nil, = {}) MongoDoc::BSON.decode(_collection.find_one(spec_or_object_id, )) end |
#insert(doc_or_docs, options = {}) ⇒ Object Also known as: <<
26 27 28 |
# File 'lib/mongodoc/collection.rb', line 26 def insert(doc_or_docs, = {}) _collection.insert(doc_or_docs.to_bson, ) end |
#save(doc, options = {}) ⇒ Object
31 32 33 |
# File 'lib/mongodoc/collection.rb', line 31 def save(doc, = {}) _collection.save(doc.to_bson, ) end |
#update(spec, doc, options = {}) ⇒ Object
35 36 37 38 39 |
# File 'lib/mongodoc/collection.rb', line 35 def update(spec, doc, = {}) _collection.update(spec, doc.to_bson, ) result = MongoDoc.database.command({'getlasterror' => 1}) (result and result.has_key?('updatedExisting')) ? result['updatedExisting'] : false end |