class MongoBrowser::Models::Collection

Attributes

mongo_collection[R]

Public Class Methods

new(mongo_collection) click to toggle source
# File lib/mongo_browser/models/collection.rb, line 6
def initialize(mongo_collection)
  @mongo_collection = mongo_collection
end

Public Instance Methods

db_name() click to toggle source

Return database name for the current collection.

# File lib/mongo_browser/models/collection.rb, line 11
def db_name
  mongo_collection.db.name
end
documents_with_pagination(page_number = 1) click to toggle source
# File lib/mongo_browser/models/collection.rb, line 32
def documents_with_pagination(page_number = 1)
  pager = Pager.new(page_number, size)

  documents = mongo_collection.find
    .skip(pager.offset)
    .limit(pager.per_page)
    .map { |document| Document.new(db_name, name, document) }

  OpenStruct.new pager.to_hash.merge(documents: documents)
end
drop!() click to toggle source
# File lib/mongo_browser/models/collection.rb, line 43
def drop!
  mongo_collection.drop
end
find(id) click to toggle source

Finds a document with the given id.

@return MongoBrowser::Models::Document

# File lib/mongo_browser/models/collection.rb, line 50
def find(id)
  condition = { _id: BSON::ObjectId(id.to_s) }
  mongo_document = mongo_collection.find(condition).first
  Document.new(db_name, name, mongo_document) if mongo_document
end
name() click to toggle source

Return collection name.

# File lib/mongo_browser/models/collection.rb, line 16
def name
  mongo_collection.name
end
remove!(document) click to toggle source

Removes the given document from the collection.

# File lib/mongo_browser/models/collection.rb, line 57
def remove!(document)
  mongo_collection.remove(_id: document.id)
end
size() click to toggle source

Return total number of documents.

# File lib/mongo_browser/models/collection.rb, line 21
def size
  mongo_collection.size
end
stats() click to toggle source

Return stats on the collection.

@return [Hash]

# File lib/mongo_browser/models/collection.rb, line 28
def stats
  mongo_collection.stats
end