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/readable.rb,
lib/mongo/collection/view/writable.rb,
lib/mongo/collection/view/iterable.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:

Defined Under Namespace

Classes: View

Instance Attribute Summary (collapse)

Instance Method Summary (collapse)

Constructor Details

- (Collection) initialize(database, name, options = {})

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



139
140
141
142
143
144
# File 'lib/mongo/collection.rb', line 139

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

Instance Attribute Details

- (Mongo::Database) 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

- (String) name (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

- (Hash) options (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

- (true, false) ==(other)

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

- (BSON::Document) bulk_write(operations, options)

Execute a batch of bulk write operations.

Examples:

Execute a bulk write.

collection.bulk_write(operations, options)

Parameters:

  • operations (Array<Hash>)

    The operations.

  • options (Hash)

    The options.

Returns:

  • (BSON::Document)

    The result of the operation.

Since:

  • 2.0.0



205
206
207
# File 'lib/mongo/collection.rb', line 205

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

- (true, false) capped?

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

- (Result) create

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

- (Result) drop

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

- (CollectionView) find(filter = nil)

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:

  • filter (Hash) (defaults to: nil)

    The filter 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(filter = nil)
  View.new(self, filter || {})
end

- (View::Index) indexes(options = {})

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

Examples:

Get the index view.

collection.indexes

Parameters:

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

    Options for getting a list of all indexes.

Returns:

  • (View::Index)

    The index view.

Since:

  • 2.0.0



125
126
127
# File 'lib/mongo/collection.rb', line 125

def indexes(options = {})
  Index::View.new(self, options)
end

- (Result) insert_many(documents, options = {})

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



184
185
186
187
188
189
190
191
192
# File 'lib/mongo/collection.rb', line 184

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

- (Result) insert_one(document, options = {})

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



169
170
171
# File 'lib/mongo/collection.rb', line 169

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

- (String) inspect

Get a pretty printed string inspection for the collection.

Examples:

Inspect the collection.

collection.inspect

Returns:

  • (String)

    The collection inspection.

Since:

  • 2.0.0



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

def inspect
  "<Mongo::Collection:0x#{object_id} namespace=#{namespace}>"
end

- (String) namespace

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



217
218
219
# File 'lib/mongo/collection.rb', line 217

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