Class: Mongo::Collection

Inherits:
Object
  • Object
show all
Extended by:
Forwardable
Defined in:
lib/mongo/collection.rb,
lib/mongo/collection/view.rb,
lib/mongo/collection/view/iterable.rb,
lib/mongo/collection/view/readable.rb,
lib/mongo/collection/view/writable.rb,
lib/mongo/collection/view/immutable.rb,
lib/mongo/collection/view/map_reduce.rb,
lib/mongo/collection/view/aggregation.rb,
lib/mongo/collection/view/explainable.rb

Overview

Represents a collection in the database and operations that can directly be applied to one.

Since:

  • 2.0.0

Defined Under Namespace

Classes: InvalidName, View

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(database, name, options = {}) ⇒ Collection

Instantiate a new collection.

Examples:

Instantiate a new collection.

Mongo::Collection.new(database, 'test')

Parameters:

  • database (Mongo::Database)

    The collection's database.

  • name (String, Symbol)

    The collection name.

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

    The collection options.

Raises:

Since:

  • 2.0.0



137
138
139
140
141
142
# File 'lib/mongo/collection.rb', line 137

def initialize(database, name, options = {})
  raise InvalidName.new unless name
  @database = database
  @name = name.to_s.freeze
  @options = options.freeze
end

Instance Attribute Details

#databaseMongo::Database (readonly)

Returns The database the collection resides in.

Returns:

Since:

  • 2.0.0



27
28
29
# File 'lib/mongo/collection.rb', line 27

def database
  @database
end

#nameString (readonly)

Returns The name of the collection.

Returns:

  • (String)

    The name of the collection.

Since:

  • 2.0.0



30
31
32
# File 'lib/mongo/collection.rb', line 30

def name
  @name
end

#optionsHash (readonly)

Returns The collection options.

Returns:

  • (Hash)

    The collection options.

Since:

  • 2.0.0



33
34
35
# File 'lib/mongo/collection.rb', line 33

def options
  @options
end

Instance Method Details

#==(other) ⇒ true, false

Check if a collection is equal to another object. Will check the name and the database for equality.

Examples:

Check collection equality.

collection == other

Parameters:

  • other (Object)

    The object to check.

Returns:

  • (true, false)

    If the objects are equal.

Since:

  • 2.0.0



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

def ==(other)
  return false unless other.is_a?(Collection)
  name == other.name && database == other.database && options == other.options
end

#bulk_write(operations, options) ⇒ Object

Since:

  • 2.0.0



180
181
182
183
# File 'lib/mongo/collection.rb', line 180

def bulk_write(operations, options)
  bulk = BulkWrite.new(operations, options, self)
  bulk.execute
end

#capped?true, false

Is the collection capped?

Examples:

Is the collection capped?

collection.capped?

Returns:

  • (true, false)

    If the collection is capped.

Since:

  • 2.0.0



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

def capped?
  database.command(:collstats => name).documents[0]['capped']
end

#createResult

Force the collection to be created in the database.

Examples:

Force the collection to be created.

collection.create

Returns:

  • (Result)

    The result of the command.

Since:

  • 2.0.0



80
81
82
# File 'lib/mongo/collection.rb', line 80

def create
  database.command({ :create => name }.merge(options))
end

#dropResult

Drop the collection. Will also drop all indexes associated with the collection.

Examples:

Drop the collection.

collection.drop

Returns:

  • (Result)

    The result of the command.

Since:

  • 2.0.0



93
94
95
# File 'lib/mongo/collection.rb', line 93

def drop
  database.command(:drop => name)
end

#find(selector = nil) ⇒ CollectionView

Find documents in the collection.

Examples:

Find documents in the collection by a selector.

collection.find(name: 1)

Get all documents in a collection.

collection.find

Parameters:

  • selector (Hash) (defaults to: nil)

    The selector to use in the find.

Returns:

  • (CollectionView)

    The collection view.

Since:

  • 2.0.0



110
111
112
# File 'lib/mongo/collection.rb', line 110

def find(selector = nil)
  View.new(self, selector || {})
end

#indexesView::Index

Get a view of all indexes for this collection. Can be iterated or has more operations.

Examples:

Get the index view.

collection.indexes

Returns:

  • (View::Index)

    The index view.

Since:

  • 2.0.0



123
124
125
# File 'lib/mongo/collection.rb', line 123

def indexes
  Index::View.new(self)
end

#insert_many(documents, options = {}) ⇒ Result

Insert the provided documents into the collection.

Examples:

Insert documents into the collection.

collection.insert_many([{ name: 'test' }])

Parameters:

  • documents (Array<Hash>)

    The documents to insert.

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

    The insert options.

Returns:

  • (Result)

    The database response wrapper.

Since:

  • 2.0.0



170
171
172
173
174
175
176
177
178
# File 'lib/mongo/collection.rb', line 170

def insert_many(documents, options = {})
  Operation::Write::Insert.new(
    :documents => documents,
    :db_name => database.name,
    :coll_name => name,
    :write_concern => write_concern,
    :options => options
  ).execute(next_primary.context)
end

#insert_one(document, options = {}) ⇒ Result

Insert a single document into the collection.

Examples:

Insert a document into the collection.

collection.insert_one({ name: 'test' })

Parameters:

  • document (Hash)

    The document to insert.

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

    The insert options.

Returns:

  • (Result)

    The database response wrapper.

Since:

  • 2.0.0



155
156
157
# File 'lib/mongo/collection.rb', line 155

def insert_one(document, options = {})
  insert_many([ document ], options)
end

#namespaceString

Get the fully qualified namespace of the collection.

Examples:

Get the fully qualified namespace.

collection.namespace

Returns:

  • (String)

    The collection namespace.

Since:

  • 2.0.0



193
194
195
# File 'lib/mongo/collection.rb', line 193

def namespace
  "#{name}.#{database.name}"
end