Sha256: 9d72eb7ce26c2b42e757720517663b2d869fa21a762291923dbab8da51465c09

Contents?: true

Size: 1.27 KB

Versions: 2

Compression:

Stored size: 1.27 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.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

2 entries across 2 versions & 1 rubygems

Version Path
mongodoc-0.2.1 lib/mongodoc/collection.rb
mongodoc-0.2.0 lib/mongodoc/collection.rb