class Monga::Collection
Attributes
collection_name[R]
Public Class Methods
new(db, collection_name)
click to toggle source
# File lib/monga/collection.rb, line 5 def initialize(db, collection_name) @db = db @collection_name = collection_name end
Public Instance Methods
aggregate(pipeline) { |err, resp| ... }
click to toggle source
# File lib/monga/collection.rb, line 146 def aggregate(pipeline, &blk) @db.aggregate(@collection_name, pipeline) do |err, resp| if block_given? yield(err, resp) else raise err if err return resp end end end
count(opts = {}) { |err, resp| ... }
click to toggle source
You could pass query/limit/skip options
count(query: {artist: "Madonna"}, limit: 10, skip: 0)
# File lib/monga/collection.rb, line 124 def count(opts = {}) @db.count(@collection_name, opts) do |err, resp| if block_given? yield(err, resp) else raise err if err return resp end end end
delete(query = {}, opts = {})
click to toggle source
# File lib/monga/collection.rb, line 58 def delete(query = {}, opts = {}) options = {} options[:query] = query options.merge!(opts) Monga::Protocol::Delete.new(connection, db_name, collection_name, options).perform end
Also aliased as: remove
distinct(opts) { |err, resp| ... }
click to toggle source
# File lib/monga/collection.rb, line 157 def distinct(opts, &blk) @db.distinct(collection_name, opts) do |err, resp| if block_given? yield(err, resp) else raise err if err return resp end end end
drop() { |err, resp| ... }
click to toggle source
# File lib/monga/collection.rb, line 109 def drop @db.drop_collection(@collection_name) do |err, resp| if block_given? yield(err, resp) else raise err if err return resp end end end
drop_index(indexes) { |err, resp| ... }
click to toggle source
# File lib/monga/collection.rb, line 76 def drop_index(indexes) @db.drop_indexes(@collection_name, indexes) do |err, resp| if block_given? yield(err, resp) else raise err if err return resp end end end
drop_indexes() { |err, resp| ... }
click to toggle source
# File lib/monga/collection.rb, line 87 def drop_indexes @db.drop_indexes(@collection_name, "*") do |err, resp| if block_given? yield(err, resp) else raise err if err return resp end end end
ensure_index(keys, opts={})
click to toggle source
# File lib/monga/collection.rb, line 66 def ensure_index(keys, opts={}) doc = {} doc[:key] = keys # Read docs about naming doc[:name] ||= keys.to_a.flatten * "_" doc[:ns] = "#{db_name}.#{collection_name}" doc.merge!(opts) Monga::Protocol::Insert.new(connection, db_name, "system.indexes", {documents: doc}).perform end
find_one(query = {}, selector = {}, opts = {}) { |err, resp| ... }
click to toggle source
# File lib/monga/collection.rb, line 28 def find_one(query = {}, selector = {}, opts = {}) options = {} options[:query] = query options[:selector] = selector options.merge!(opts) Monga::Cursor.create(connection, db_name, collection_name, options).first do |err, resp| if block_given? yield(err, resp) else err ? raise(err) : resp end end end
Also aliased as: first
get_indexes() { |err, resp| ... }
click to toggle source
# File lib/monga/collection.rb, line 98 def get_indexes Monga::Cursor.create(connection, db_name, "system.indexes").all do |err, resp| if block_given? yield(err, resp) else raise err if err return resp end end end
group(opts) { |err, resp| ... }
click to toggle source
# File lib/monga/collection.rb, line 168 def group(opts, &blk) @db.group(collection_name, opts) do |err, resp| if block_given? yield(err, resp) else raise err if err return resp end end end
insert(document, opts = {})
click to toggle source
# File lib/monga/collection.rb, line 43 def insert(document, opts = {}) options = {} options[:documents] = document options.merge!(opts) Monga::Protocol::Insert.new(connection, db_name, collection_name, options).perform end
map_reduce(opts) { |err, resp| ... }
click to toggle source
# File lib/monga/collection.rb, line 135 def map_reduce(opts, &blk) @db.map_reduce(@collection_name, opts) do |err, resp| if block_given? yield(err, resp) else raise err if err return resp end end end
query(query = {}, selector = {}, opts = {})
click to toggle source
Querying database. It returns cursor. Alias to collection#query is collection#find
cursor = collection.find(title: "Madonna") # choose fields to return cursor = collection.find({ title: "Madonna" }, { track: 1 }) # get all documents cursor.all{ |err, docs| docs.each{ |doc| puts doc } }
# File lib/monga/collection.rb, line 19 def query(query = {}, selector = {}, opts = {}) options = {} options[:query] = query options[:selector] = selector options.merge!(opts) Monga::Cursor.create(connection, db_name, collection_name, options) end
Also aliased as: find
text(search, opts = {}) { |err, resp| ... }
click to toggle source
# File lib/monga/collection.rb, line 179 def text(search, opts = {}, &blk) opts[:search] = search @db.text(collection_name, opts) do |err, resp| if block_given? yield(err, resp) else raise err if err return resp end end end
update(query = {}, update = {}, flags = {})
click to toggle source
# File lib/monga/collection.rb, line 50 def update(query = {}, update = {}, flags = {}) options = {} options[:query] = query options[:update] = update options.merge!(flags) Monga::Protocol::Update.new(connection, db_name, collection_name, options).perform end
Private Instance Methods
connection()
click to toggle source
# File lib/monga/collection.rb, line 217 def connection @db.client.aquire_connection end
db_name()
click to toggle source
# File lib/monga/collection.rb, line 221 def db_name @db.name end