Class: Moped::Collection

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

Overview

The class for interacting with a MongoDB collection.

Since:

  • 1.0.0

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



101
102
103
104
# File 'lib/moped/collection.rb', line 101

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

Instance Attribute Details

#databaseDatabase

Returns The database for the collection.

Returns:

  • (Database)

    The database for the collection.



16
17
18
# File 'lib/moped/collection.rb', line 16

def database
  @database
end

#nameObject

Since:

  • 1.0.0



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

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



145
146
147
# File 'lib/moped/collection.rb', line 145

def aggregate(*pipeline)
  session.command(aggregate: name, pipeline: pipeline.flatten)["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



26
27
28
# File 'lib/moped/collection.rb', line 26

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



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

def drop
  begin
    session.with(read: :primary).command(drop: name)
  rescue Moped::Errors::OperationFailure => e
    raise e unless e.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



75
76
77
# File 'lib/moped/collection.rb', line 75

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



88
89
90
# File 'lib/moped/collection.rb', line 88

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



122
123
124
125
126
127
# File 'lib/moped/collection.rb', line 122

def insert(documents, flags = nil)
  docs = documents.is_a?(Array) ? documents : [ documents ]
  cluster.with_primary do |node|
    node.insert(database.name, name, docs, write_concern, flags: flags || [])
  end
end

#rename(to_name) ⇒ Hash

Rename the collection

Examples:

Rename the collection to ‘foo’

collection.rename('foo')

Returns:

  • (Hash)

    The command information.

Since:

  • 2.0.0



55
56
57
58
59
60
61
62
63
64
# File 'lib/moped/collection.rb', line 55

def rename(to_name)
  begin
    session.
      with(database: "admin", read: :primary).
      command(renameCollection: "#{database.name}.#{name}", to: "#{database.name}.#{to_name}")
  rescue Moped::Errors::OperationFailure => e
    raise e unless e.ns_not_exists?
    false
  end
end

#sessionSession

Get the session for the collection.

Examples:

Get the session for the collection.

collection.session

Returns:

  • (Session)

    The session for the collection.

Since:

  • 2.0.0



157
158
159
# File 'lib/moped/collection.rb', line 157

def session
  database.session
end

#write_concernObject

Since:

  • 1.0.0



161
162
163
# File 'lib/moped/collection.rb', line 161

def write_concern
  session.write_concern
end