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/iterable.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: 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')

Raises:

Since:

  • 2.0.0



144
145
146
147
148
149
# File 'lib/mongo/collection.rb', line 144

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

#databaseMongo::Database (readonly)

Returns The database the collection resides in.

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.

Since:

  • 2.0.0



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

def name
  @name
end

#optionsHash (readonly)

Returns 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

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) ⇒ BSON::Document

Execute a batch of bulk write operations.

Examples:

Execute a bulk write.

collection.bulk_write(operations, options)

Since:

  • 2.0.0



210
211
212
# File 'lib/mongo/collection.rb', line 210

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

#capped?true, false

Is the collection capped?

Examples:

Is the collection capped?

collection.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

Since:

  • 2.0.0



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

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

#dropResult

Note:

An error returned if the collection doesn't exist is suppressed.

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

Examples:

Drop the collection.

collection.drop

Since:

  • 2.0.0



95
96
97
98
99
100
# File 'lib/mongo/collection.rb', line 95

def drop
  database.command(:drop => name)
rescue Error::OperationFailure => ex
  raise ex unless ex.message =~ /ns not found/
  false
end

#find(filter = 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

Since:

  • 2.0.0



115
116
117
# File 'lib/mongo/collection.rb', line 115

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

#indexes(options = {}) ⇒ View::Index

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

Examples:

Get the index view.

collection.indexes

Since:

  • 2.0.0



130
131
132
# File 'lib/mongo/collection.rb', line 130

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

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

Insert the provided documents into the collection.

Examples:

Insert documents into the collection.

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

Since:

  • 2.0.0



189
190
191
192
193
194
195
196
197
# File 'lib/mongo/collection.rb', line 189

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' })

Since:

  • 2.0.0



174
175
176
# File 'lib/mongo/collection.rb', line 174

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

#inspectString

Get a pretty printed string inspection for the collection.

Examples:

Inspect the collection.

collection.inspect

Since:

  • 2.0.0



159
160
161
# File 'lib/mongo/collection.rb', line 159

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

#namespaceString

Get the fully qualified namespace of the collection.

Examples:

Get the fully qualified namespace.

collection.namespace

Since:

  • 2.0.0



222
223
224
# File 'lib/mongo/collection.rb', line 222

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