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



68
69
70
# File 'lib/moped/collection.rb', line 68

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

#dropHash

Drop the collection.

Examples:

Drop the collection.

collection.drop

Returns:

  • (Hash)

    The command information.

Since:

  • 1.0.0



24
25
26
27
28
29
30
# File 'lib/moped/collection.rb', line 24

def drop
  begin
    database.command(drop: name)
  rescue Moped::Errors::OperationFailure => e
    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



42
43
44
# File 'lib/moped/collection.rb', line 42

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



55
56
57
# File 'lib/moped/collection.rb', line 55

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



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

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