Class: Mongo::Collection
- Inherits:
-
Object
- Object
- Mongo::Collection
- Extended by:
- Forwardable
- Includes:
- Retryable
- 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,
lib/mongo/collection/view/builder/flags.rb,
lib/mongo/collection/view/builder/op_query.rb,
lib/mongo/collection/view/builder/modifiers.rb,
lib/mongo/collection/view/builder/map_reduce.rb,
lib/mongo/collection/view/builder/aggregation.rb,
lib/mongo/collection/view/builder/find_command.rb
Overview
Represents a collection in the database and operations that can directly be applied to one.
Defined Under Namespace
Classes: View
Constant Summary collapse
- CAPPED =
The capped option.
'capped'.freeze
- NS =
The ns field constant.
'ns'.freeze
- CHANGEABLE_OPTIONS =
Options that can be updated on a new Collection instance via the #with method.
[ :read, :read_concern, :write ].freeze
Constants included from Retryable
Retryable::COULD_NOT_CONTACT_PRIMARY, Retryable::NOT_MASTER
Instance Attribute Summary collapse
-
#database ⇒ Mongo::Database
readonly
The database the collection resides in.
-
#name ⇒ String
readonly
The name of the collection.
-
#options ⇒ Hash
readonly
The collection options.
Instance Method Summary collapse
-
#==(other) ⇒ true, false
Check if a collection is equal to another object.
-
#aggregate(pipeline, options = {}) ⇒ Aggregation
Perform an aggregation on the collection.
-
#bulk_write(requests, options = {}) ⇒ BulkWrite::Result
Execute a batch of bulk write operations.
-
#capped? ⇒ true, false
Is the collection capped?.
-
#count(filter = nil, options = {}) ⇒ Integer
Get a count of matching documents in the collection.
-
#create ⇒ Result
Force the collection to be created in the database.
-
#delete_many(filter = nil, options = {}) ⇒ Result
Remove documents from the collection.
-
#delete_one(filter = nil, options = {}) ⇒ Result
Remove a document from the collection.
-
#distinct(field_name, filter = nil, options = {}) ⇒ Array<Object>
Get a list of distinct values for a specific field.
-
#drop ⇒ Result
Drop the collection.
-
#find(filter = nil, options = {}) ⇒ CollectionView
Find documents in the collection.
-
#find_one_and_delete(filter, options = {}) ⇒ BSON::Document?
Finds a single document in the database via findAndModify and deletes it, returning the original document.
-
#find_one_and_replace(filter, replacement, options = {}) ⇒ BSON::Document
Finds a single document and replaces it, returning the original doc unless otherwise specified.
-
#find_one_and_update(filter, update, options = {}) ⇒ BSON::Document
Finds a single document via findAndModify and updates it, returning the original doc unless otherwise specified.
-
#indexes(options = {}) ⇒ View::Index
Get a view of all indexes for this collection.
-
#initialize(database, name, options = {}) ⇒ Collection
constructor
Instantiate a new collection.
-
#insert_many(documents, options = {}) ⇒ Result
Insert the provided documents into the collection.
-
#insert_one(document, options = {}) ⇒ Result
Insert a single document into the collection.
-
#inspect ⇒ String
Get a pretty printed string inspection for the collection.
-
#namespace ⇒ String
Get the fully qualified namespace of the collection.
-
#parallel_scan(cursor_count, options = {}) ⇒ Array<Cursor>
Execute a parallel scan on the collection view.
-
#read_concern ⇒ Hash
Get the read concern for this collection instance.
-
#read_preference ⇒ Mongo::ServerSelector
Get the read preference on this collection.
-
#replace_one(filter, replacement, options = {}) ⇒ Result
Replaces a single document in the collection with the new document.
-
#update_many(filter, update, options = {}) ⇒ Result
Update documents in the collection.
-
#update_one(filter, update, options = {}) ⇒ Result
Update a single document in the collection.
-
#with(new_options) ⇒ Mongo::Collection
A new collection instance.
-
#write_concern ⇒ Mongo::WriteConcern
Get the write concern on this collection.
Methods included from Retryable
#read_with_one_retry, #read_with_retry, #write_with_retry
Constructor Details
#initialize(database, name, options = {}) ⇒ Collection
Instantiate a new collection.
87 88 89 90 91 92 |
# File 'lib/mongo/collection.rb', line 87 def initialize(database, name, = {}) raise Error::InvalidCollectionName.new unless name @database = database @name = name.to_s.freeze = .freeze end |
Instance Attribute Details
#database ⇒ Mongo::Database (readonly)
Returns The database the collection resides in.
39 40 41 |
# File 'lib/mongo/collection.rb', line 39 def database @database end |
#name ⇒ String (readonly)
Returns The name of the collection.
42 43 44 |
# File 'lib/mongo/collection.rb', line 42 def name @name end |
#options ⇒ Hash (readonly)
Returns The collection options.
45 46 47 |
# File 'lib/mongo/collection.rb', line 45 def 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.
72 73 74 75 |
# File 'lib/mongo/collection.rb', line 72 def ==(other) return false unless other.is_a?(Collection) name == other.name && database == other.database && == other. end |
#aggregate(pipeline, options = {}) ⇒ Aggregation
Perform an aggregation on the collection.
271 272 273 |
# File 'lib/mongo/collection.rb', line 271 def aggregate(pipeline, = {}) View.new(self, {}).aggregate(pipeline, ) end |
#bulk_write(requests, options = {}) ⇒ BulkWrite::Result
Execute a batch of bulk write operations.
403 404 405 |
# File 'lib/mongo/collection.rb', line 403 def bulk_write(requests, = {}) BulkWrite.new(self, requests, ).execute end |
#capped? ⇒ true, false
Is the collection capped?
159 160 161 |
# File 'lib/mongo/collection.rb', line 159 def capped? database.command(:collstats => name).documents[0][CAPPED] end |
#count(filter = nil, options = {}) ⇒ Integer
Get a count of matching documents in the collection.
293 294 295 |
# File 'lib/mongo/collection.rb', line 293 def count(filter = nil, = {}) View.new(self, filter || {}, ).count() end |
#create ⇒ Result
Force the collection to be created in the database.
171 172 173 174 175 176 177 178 179 180 181 182 183 |
# File 'lib/mongo/collection.rb', line 171 def create operation = { :create => name }.merge() operation.delete(:write) server = next_primary if ([:collation] || [Operation::COLLATION]) && !server.features.collation_enabled? raise Error::UnsupportedCollation.new end Operation::Commands::Create.new({ selector: operation, db_name: database.name, write_concern: write_concern }).execute(server) end |
#delete_many(filter = nil, options = {}) ⇒ Result
Remove documents from the collection.
437 438 439 |
# File 'lib/mongo/collection.rb', line 437 def delete_many(filter = nil, = {}) find(filter, ).delete_many() end |
#delete_one(filter = nil, options = {}) ⇒ Result
Remove a document from the collection.
420 421 422 |
# File 'lib/mongo/collection.rb', line 420 def delete_one(filter = nil, = {}) find(filter, ).delete_one() end |
#distinct(field_name, filter = nil, options = {}) ⇒ Array<Object>
Get a list of distinct values for a specific field.
313 314 315 |
# File 'lib/mongo/collection.rb', line 313 def distinct(field_name, filter = nil, = {}) View.new(self, filter || {}, ).distinct(field_name, ) end |
#drop ⇒ Result
An error returned if the collection doesn’t exist is suppressed.
Drop the collection. Will also drop all indexes associated with the collection.
196 197 198 199 200 201 202 203 204 205 206 |
# File 'lib/mongo/collection.rb', line 196 def drop Operation::Commands::Drop.new({ selector: { :drop => name }, db_name: database.name, write_concern: write_concern }).execute(next_primary) rescue Error::OperationFailure => ex raise ex unless ex. =~ /ns not found/ false end |
#find(filter = nil, options = {}) ⇒ CollectionView
Find documents in the collection.
245 246 247 |
# File 'lib/mongo/collection.rb', line 245 def find(filter = nil, = {}) View.new(self, filter || {}, ) end |
#find_one_and_delete(filter, options = {}) ⇒ BSON::Document?
Finds a single document in the database via findAndModify and deletes it, returning the original document.
550 551 552 |
# File 'lib/mongo/collection.rb', line 550 def find_one_and_delete(filter, = {}) find(filter, ).find_one_and_delete end |
#find_one_and_replace(filter, replacement, options = {}) ⇒ BSON::Document
Finds a single document and replaces it, returning the original doc unless otherwise specified.
616 617 618 |
# File 'lib/mongo/collection.rb', line 616 def find_one_and_replace(filter, replacement, = {}) find(filter, ).find_one_and_update(replacement, ) end |
#find_one_and_update(filter, update, options = {}) ⇒ BSON::Document
Finds a single document via findAndModify and updates it, returning the original doc unless otherwise specified.
583 584 585 |
# File 'lib/mongo/collection.rb', line 583 def find_one_and_update(filter, update, = {}) find(filter, ).find_one_and_update(update, ) end |
#indexes(options = {}) ⇒ View::Index
Get a view of all indexes for this collection. Can be iterated or has more operations.
328 329 330 |
# File 'lib/mongo/collection.rb', line 328 def indexes( = {}) Index::View.new(self, ) end |
#insert_many(documents, options = {}) ⇒ Result
Insert the provided documents into the collection.
380 381 382 383 |
# File 'lib/mongo/collection.rb', line 380 def insert_many(documents, = {}) inserts = documents.map{ |doc| { :insert_one => doc }} bulk_write(inserts, ) end |
#insert_one(document, options = {}) ⇒ Result
Insert a single document into the collection.
355 356 357 358 359 360 361 362 363 364 365 366 367 |
# File 'lib/mongo/collection.rb', line 355 def insert_one(document, = {}) write_with_retry do Operation::Write::Insert.new( :documents => [ document ], :db_name => database.name, :coll_name => name, :write_concern => write_concern, :bypass_document_validation => !![:bypass_document_validation], :options => , :id_generator => client.[:id_generator] ).execute(next_primary) end end |
#inspect ⇒ String
Get a pretty printed string inspection for the collection.
340 341 342 |
# File 'lib/mongo/collection.rb', line 340 def inspect "#<Mongo::Collection:0x#{object_id} namespace=#{namespace}>" end |
#namespace ⇒ String
Get the fully qualified namespace of the collection.
628 629 630 |
# File 'lib/mongo/collection.rb', line 628 def namespace "#{database.name}.#{name}" end |
#parallel_scan(cursor_count, options = {}) ⇒ Array<Cursor>
Execute a parallel scan on the collection view.
Returns a list of up to cursor_count cursors that can be iterated concurrently. As long as the collection is not modified during scanning, each document appears once in one of the cursors’ result sets.
459 460 461 |
# File 'lib/mongo/collection.rb', line 459 def parallel_scan(cursor_count, = {}) find.send(:parallel_scan, cursor_count, ) end |
#read_concern ⇒ Hash
Get the read concern for this collection instance.
102 103 104 |
# File 'lib/mongo/collection.rb', line 102 def read_concern @read_concern ||= [:read_concern] end |
#read_preference ⇒ Mongo::ServerSelector
Get the read preference on this collection.
114 115 116 |
# File 'lib/mongo/collection.rb', line 114 def read_preference @read_preference ||= ServerSelector.get([:read] || database.read_preference) end |
#replace_one(filter, replacement, options = {}) ⇒ Result
Replaces a single document in the collection with the new document.
481 482 483 |
# File 'lib/mongo/collection.rb', line 481 def replace_one(filter, replacement, = {}) find(filter, ).replace_one(replacement, ) end |
#update_many(filter, update, options = {}) ⇒ Result
Update documents in the collection.
503 504 505 |
# File 'lib/mongo/collection.rb', line 503 def update_many(filter, update, = {}) find(filter, ).update_many(update, ) end |
#update_one(filter, update, options = {}) ⇒ Result
Update a single document in the collection.
525 526 527 |
# File 'lib/mongo/collection.rb', line 525 def update_one(filter, update, = {}) find(filter, ).update_one(update, ) end |
#with(new_options) ⇒ Mongo::Collection
Returns A new collection instance.
144 145 146 147 148 149 |
# File 'lib/mongo/collection.rb', line 144 def with() .keys.each do |k| raise Error::UnchangeableCollectionOption.new(k) unless CHANGEABLE_OPTIONS.include?(k) end Collection.new(database, name, .merge()) end |
#write_concern ⇒ Mongo::WriteConcern
Get the write concern on this collection.
126 127 128 |
# File 'lib/mongo/collection.rb', line 126 def write_concern @write_concern ||= WriteConcern.get([:write] || database.write_concern) end |