Sha256: 9476a1b1784f20880c00109e66f23e9d7e4292fc87ba1c0385aebaa81834ed20

Contents?: true

Size: 1.3 KB

Versions: 3

Compression:

Stored size: 1.3 KB

Contents

require 'mongodoc/cursor'

module MongoDoc
  class Collection
    attr_accessor :_collection
    delegate :[], :clear, :count, :create_index, :db, :drop, :drop_index, :drop_indexes, :group, :hint, :index_information, :name, :options, :remove, :rename, :size, :to => :_collection
    
    def initialize(name)
      self._collection = self.class.mongo_collection(name)
    end
    
    def find(query = {}, options = {})
      cursor = MongoDoc::Cursor.new(_collection.find(query, options))
      if block_given?
        yield cursor
        cursor.close
      else
        cursor
      end
    end
    
    def find_one(spec_or_object_id = nil, options = {})
      MongoDoc::BSON.decode(_collection.find_one(spec_or_object_id, options))
    end
    
    def insert(doc_or_docs, options = {})
      _collection.insert(doc_or_docs.to_bson, options)
    end
    alias :<< :insert
    
    def save(doc, options = {})
      _collection.save(doc.to_bson, options)
    end
    
    def update(spec, doc, options = {})
      _collection.update(spec, doc.to_bson, options)
      result = MongoDoc.database.db_command({'getlasterror' => 1})
      (result and result.has_key?('updatedExisting')) ? result['updatedExisting'] : false
    end
    
    def self.mongo_collection(name)
      MongoDoc.database.collection(name)
    end
  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
mongodoc-0.1.2 lib/mongodoc/collection.rb
mongodoc-0.1.1 lib/mongodoc/collection.rb
mongodoc-0.1.0 lib/mongodoc/collection.rb