Class: Moped::Collection

Inherits:
Object show all
Defined in:
lib/moped/collection.rb

Overview

The class for interacting with a MongoDB collection.

Examples:

users = session[:users] # => <Moped::Collection ...>
users.drop
users.insert(name: "John")
users.find.to_a # => [{ name: "John" }]

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(database, name) ⇒ Collection

Initialize the new collection.

Examples:

Initialize the collection.

Collection.new(database, :artists)

Parameters:

  • database (Database)

    The collection’s database.

  • name (String, Symbol)

    The collection name.

Since:

  • 1.0.0



83
84
85
# File 'lib/moped/collection.rb', line 83

def initialize(database, name)
  @database, @name = database, name.to_s
end

Instance Attribute Details

#databaseObject (readonly)

Returns the value of attribute database.



14
15
16
# File 'lib/moped/collection.rb', line 14

def database
  @database
end

#database The collection's database.(Thecollection's database.) ⇒ Object (readonly)



14
# File 'lib/moped/collection.rb', line 14

attr_reader :database, :name

#nameObject (readonly)

Returns the value of attribute name.



14
15
16
# File 'lib/moped/collection.rb', line 14

def name
  @name
end

#name The collection name.(Thecollectionname.) ⇒ Object (readonly)



14
# File 'lib/moped/collection.rb', line 14

attr_reader :database, :name

Instance Method Details

#aggregate(*pipeline) ⇒ Hash

Call aggregate function over the collection.

Examples:

Execute an aggregation.

session[:users].aggregate({
  "$group" => {
    "_id" => "$city",
    "totalpop" => { "$sum" => "$pop" }
  }
})

Parameters:

  • documents (Hash, Array<Hash>)

    representing the aggregate function to execute

Returns:

  • (Hash)

    containing the result of aggregation

Since:

  • 1.3.0



125
126
127
128
129
# File 'lib/moped/collection.rb', line 125

def aggregate(*pipeline)
  pipeline.flatten!
  command = { aggregate: name.to_s, pipeline: pipeline }
  database.session.command(command)["result"]
end

#capped?true, false

Return whether or not this collection is a capped collection.

Examples:

Is the collection capped?

collection.capped?

Returns:

  • (true, false)

    If the collection is capped.

Since:

  • 1.4.0



24
25
26
# File 'lib/moped/collection.rb', line 24

def capped?
  database.command(collstats: name)["capped"]
end

#dropHash

Drop the collection.

Examples:

Drop the collection.

collection.drop

Returns:

  • (Hash)

    The command information.

Since:

  • 1.0.0



36
37
38
39
40
41
42
43
44
45
# File 'lib/moped/collection.rb', line 36

def drop
  begin
    database.session.with(consistency: :strong) do |session|
      session.context.command(database.name, drop: name)
    end
  rescue Moped::Errors::OperationFailure => e
    raise e unless e.details["errmsg"] == "ns not found"
    false
  end
end

#find(selector = {}) ⇒ Query Also known as: where

Build a query for this collection.

Examples:

Build a query based on the provided selector.

collection.find(name: "Placebo")

Parameters:

  • selector (Hash) (defaults to: {})

    The query selector.

Returns:

  • (Query)

    The generated query.

Since:

  • 1.0.0



57
58
59
# File 'lib/moped/collection.rb', line 57

def find(selector = {})
  Query.new(self, selector)
end

#indexesIndexes

Access information about this collection’s indexes.

Examples:

Get the index information.

collection.indexes

Returns:

  • (Indexes)

    The index information.

Since:

  • 1.0.0



70
71
72
# File 'lib/moped/collection.rb', line 70

def indexes
  Indexes.new(database, name)
end

#insert(documents, flags = nil) ⇒ nil

Insert one or more documents into the collection.

Examples:

Insert a single document.

db[:people].insert(name: "John")

Insert multiple documents in batch.

db[:people].insert([{name: "John"}, {name: "Joe"}])

Parameters:

  • documents (Hash, Array<Hash>)

    The document(s) to insert.

  • flags (Array) (defaults to: nil)

    The flags, valid values are :continue_on_error.

  • options (Hash)

    a customizable set of options

Returns:

  • (nil)

    nil.

Since:

  • 1.0.0



103
104
105
106
107
108
# File 'lib/moped/collection.rb', line 103

def insert(documents, flags = nil)
  documents = [documents] unless documents.is_a?(Array)
  database.session.with(consistency: :strong) do |session|
    session.context.insert(database.name, name, documents, flags: flags || [])
  end
end