Class: MongoDoc::Collection
- Includes:
- Criteria
- Defined in:
- lib/mongo_doc/collection.rb
Instance Attribute Summary collapse
-
#_collection ⇒ Object
Returns the value of attribute _collection.
Instance Method Summary collapse
- #find(query = {}, options = {}) ⇒ Object
- #find_and_modify(opts) ⇒ 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
Methods included from Criteria
Constructor Details
#initialize(name) ⇒ Collection
Returns a new instance of Collection.
32 33 34 |
# File 'lib/mongo_doc/collection.rb', line 32 def initialize(name) self._collection = self.class.mongo_collection(name) end |
Instance Attribute Details
#_collection ⇒ Object
Returns the value of attribute _collection.
6 7 8 |
# File 'lib/mongo_doc/collection.rb', line 6 def _collection @_collection end |
Instance Method Details
#find(query = {}, options = {}) ⇒ Object
36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 |
# File 'lib/mongo_doc/collection.rb', line 36 def find(query = {}, = {}) retried = false begin cursor = wrapped_cursor(query, ) if block_given? yield cursor cursor.close else cursor end rescue Mongo::ConnectionFailure raise if retried retried = true retry end end |
#find_and_modify(opts) ⇒ Object
53 54 55 |
# File 'lib/mongo_doc/collection.rb', line 53 def find_and_modify(opts) MongoDoc::BSON.decode(_collection.find_and_modify(opts)) end |
#find_one(spec_or_object_id = nil, options = {}) ⇒ Object
57 58 59 60 61 62 63 64 65 66 |
# File 'lib/mongo_doc/collection.rb', line 57 def find_one(spec_or_object_id = nil, = {}) retried = false begin MongoDoc::BSON.decode(_collection.find_one(spec_or_object_id, )) rescue Mongo::ConnectionFailure raise if retried retried = true retry end end |
#insert(doc_or_docs, options = {}) ⇒ Object Also known as: <<
68 69 70 |
# File 'lib/mongo_doc/collection.rb', line 68 def insert(doc_or_docs, = {}) _collection.insert(doc_or_docs.to_bson, ) end |
#save(doc, options = {}) ⇒ Object
73 74 75 |
# File 'lib/mongo_doc/collection.rb', line 73 def save(doc, = {}) _collection.save(doc.to_bson, ) end |
#update(spec, doc, options = {}) ⇒ Object
77 78 79 80 |
# File 'lib/mongo_doc/collection.rb', line 77 def update(spec, doc, = {}) _collection.update(spec, doc.to_bson, ) (last_error || {})['updatedExisting'] || false end |