Class: Moped::Collection

Inherits:
Object
  • Object
show all
Includes:
Readable, Retryable
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



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

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.



18
19
20
# File 'lib/moped/collection.rb', line 18

def database
  @database
end

#nameObject

Since:

  • 1.0.0



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

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



149
150
151
# File 'lib/moped/collection.rb', line 149

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



28
29
30
# File 'lib/moped/collection.rb', line 28

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



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

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



77
78
79
# File 'lib/moped/collection.rb', line 77

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



90
91
92
# File 'lib/moped/collection.rb', line 90

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



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

def insert(documents, flags = nil)
  with_retry(cluster) do
    docs = documents.is_a?(Array) ? documents : [ documents ]
    cluster.with_primary do |node|
      node.insert(database.name, name, docs, write_concern, flags: flags || [])
    end
  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



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

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



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

def session
  database.session
end

#write_concernObject

Since:

  • 1.0.0



165
166
167
# File 'lib/moped/collection.rb', line 165

def write_concern
  session.write_concern
end