Class: MongoDoc::Collection

Inherits:
Object show all
Includes:
Criteria
Defined in:
lib/mongo_doc/collection.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Criteria

#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

#_collectionObject

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
# File 'lib/mongo_doc/collection.rb', line 36

def find(query = {}, options = {})
  cursor = wrapped_cursor(query, options)
  if block_given?
    yield cursor
    cursor.close
  else
    cursor
  end
end

#find_one(spec_or_object_id = nil, options = {}) ⇒ Object



46
47
48
# File 'lib/mongo_doc/collection.rb', line 46

def find_one(spec_or_object_id = nil, options = {})
  MongoDoc::BSON.decode(_collection.find_one(spec_or_object_id, options))
end

#insert(doc_or_docs, options = {}) ⇒ Object Also known as: <<



50
51
52
# File 'lib/mongo_doc/collection.rb', line 50

def insert(doc_or_docs, options = {})
  _collection.insert(doc_or_docs.to_bson, options)
end

#save(doc, options = {}) ⇒ Object



55
56
57
# File 'lib/mongo_doc/collection.rb', line 55

def save(doc, options = {})
  _collection.save(doc.to_bson, options)
end

#update(spec, doc, options = {}) ⇒ Object



59
60
61
62
# File 'lib/mongo_doc/collection.rb', line 59

def update(spec, doc, options = {})
  _collection.update(spec, doc.to_bson, options)
  (last_error || {})['updatedExisting'] || false
end