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/change_stream.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,
lib/mongo/collection/view/change_stream/retryable.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
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
deprecated
Deprecated.
Use #count_documents or estimated_document_count instead. However, note that the following operators will need to be substituted when switching to #count_documents:
* $where should be replaced with $expr (only works on 3.6+) * $near should be replaced with $geoWithin with $center * $nearSphere should be replaced with $geoWithin with $centerSphere -
#count_documents(filter, options = {}) ⇒ Integer
Gets the number of of matching documents in the collection.
-
#create(opts = {}) ⇒ 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(opts = {}) ⇒ Result
Drop the collection.
-
#estimated_document_count(options = {}) ⇒ Integer
Gets an estimate of the count of documents in a collection using collection metadata.
-
#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, opts = {}) ⇒ 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 ⇒ Hash
Get the read preference on this collection.
-
#replace_one(filter, replacement, options = {}) ⇒ Result
Replaces a single document in the collection with the new document.
-
#server_selector ⇒ Mongo::ServerSelector
Get the server selector on this collection.
-
#update_many(filter, update, options = {}) ⇒ Result
Update documents in the collection.
-
#update_one(filter, update, options = {}) ⇒ Result
Update a single document in the collection.
-
#watch(pipeline = [], options = {}) ⇒ ChangeStream
As of version 3.6 of the MongoDB server, a “$changeStream“ pipeline stage is supported in the aggregation framework.
-
#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.
84 85 86 87 88 89 |
# File 'lib/mongo/collection.rb', line 84 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.
69 70 71 72 |
# File 'lib/mongo/collection.rb', line 69 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.
297 298 299 |
# File 'lib/mongo/collection.rb', line 297 def aggregate(pipeline, = {}) View.new(self, {}, ).aggregate(pipeline, ) end |
#bulk_write(requests, options = {}) ⇒ BulkWrite::Result
Execute a batch of bulk write operations.
531 532 533 |
# File 'lib/mongo/collection.rb', line 531 def bulk_write(requests, = {}) BulkWrite.new(self, requests, ).execute end |
#capped? ⇒ true, false
Is the collection capped?
168 169 170 |
# File 'lib/mongo/collection.rb', line 168 def capped? database.command(:collstats => name).documents[0][CAPPED] end |
#count(filter = nil, options = {}) ⇒ Integer
Use #count_documents or estimated_document_count instead. However, note that the following operators will need to be substituted when switching to #count_documents:
* $where should be replaced with $expr (only works on 3.6+)
* $near should be replaced with $geoWithin with $center
* $nearSphere should be replaced with $geoWithin with $centerSphere
Gets the number of matching documents in the collection.
366 367 368 |
# File 'lib/mongo/collection.rb', line 366 def count(filter = nil, = {}) View.new(self, filter || {}, ).count() end |
#count_documents(filter, options = {}) ⇒ Integer
Gets the number of of matching documents in the collection. Unlike the deprecated #count method, this will return the exact number of documents matching the filter rather than the estimate.
391 392 393 |
# File 'lib/mongo/collection.rb', line 391 def count_documents(filter, = {}) View.new(self, filter, ).count_documents() end |
#create(opts = {}) ⇒ Result
Force the collection to be created in the database.
184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 |
# File 'lib/mongo/collection.rb', line 184 def create(opts = {}) operation = { :create => name }.merge() operation.delete(:write) server = next_primary if ([:collation] || [Operation::COLLATION]) && !server.features.collation_enabled? raise Error::UnsupportedCollation.new end client.send(:with_session, opts) do |session| Operation::Create.new({ selector: operation, db_name: database.name, write_concern: write_concern, session: session }).execute(server) end end |
#delete_many(filter = nil, options = {}) ⇒ Result
Remove documents from the collection.
567 568 569 |
# File 'lib/mongo/collection.rb', line 567 def delete_many(filter = nil, = {}) find(filter, ).delete_many() end |
#delete_one(filter = nil, options = {}) ⇒ Result
Remove a document from the collection.
549 550 551 |
# File 'lib/mongo/collection.rb', line 549 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.
430 431 432 |
# File 'lib/mongo/collection.rb', line 430 def distinct(field_name, filter = nil, = {}) View.new(self, filter || {}, ).distinct(field_name, ) end |
#drop(opts = {}) ⇒ Result
An error returned if the collection doesn’t exist is suppressed.
Drop the collection. Will also drop all indexes associated with the collection.
216 217 218 219 220 221 222 223 224 225 226 227 228 |
# File 'lib/mongo/collection.rb', line 216 def drop(opts = {}) client.send(:with_session, opts) do |session| Operation::Drop.new({ selector: { :drop => name }, db_name: database.name, write_concern: write_concern, session: session }).execute(next_primary) end rescue Error::OperationFailure => ex raise ex unless ex. =~ /ns not found/ false end |
#estimated_document_count(options = {}) ⇒ Integer
Gets an estimate of the count of documents in a collection using collection metadata.
409 410 411 |
# File 'lib/mongo/collection.rb', line 409 def estimated_document_count( = {}) View.new(self, {}, ).estimated_document_count() end |
#find(filter = nil, options = {}) ⇒ CollectionView
Find documents in the collection.
268 269 270 |
# File 'lib/mongo/collection.rb', line 268 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.
689 690 691 |
# File 'lib/mongo/collection.rb', line 689 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.
759 760 761 |
# File 'lib/mongo/collection.rb', line 759 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.
725 726 727 |
# File 'lib/mongo/collection.rb', line 725 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.
447 448 449 |
# File 'lib/mongo/collection.rb', line 447 def indexes( = {}) Index::View.new(self, ) end |
#insert_many(documents, options = {}) ⇒ Result
Insert the provided documents into the collection.
507 508 509 510 |
# File 'lib/mongo/collection.rb', line 507 def insert_many(documents, = {}) inserts = documents.map{ |doc| { :insert_one => doc }} bulk_write(inserts, ) end |
#insert_one(document, opts = {}) ⇒ Result
Insert a single document into the collection.
476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 |
# File 'lib/mongo/collection.rb', line 476 def insert_one(document, opts = {}) client.send(:with_session, opts) do |session| write_with_retry(session, write_concern) do |server, txn_num| Operation::Insert.new( :documents => [ document ], :db_name => database.name, :coll_name => name, :write_concern => write_concern, :bypass_document_validation => !!opts[:bypass_document_validation], :options => opts, :id_generator => client.[:id_generator], :session => session, :txn_num => txn_num ).execute(server) end end end |
#inspect ⇒ String
Get a pretty printed string inspection for the collection.
459 460 461 |
# File 'lib/mongo/collection.rb', line 459 def inspect "#<Mongo::Collection:0x#{object_id} namespace=#{namespace}>" end |
#namespace ⇒ String
Get the fully qualified namespace of the collection.
771 772 773 |
# File 'lib/mongo/collection.rb', line 771 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.
590 591 592 |
# File 'lib/mongo/collection.rb', line 590 def parallel_scan(cursor_count, = {}) find({}, ).send(:parallel_scan, cursor_count, ) end |
#read_concern ⇒ Hash
Get the read concern for this collection instance.
99 100 101 |
# File 'lib/mongo/collection.rb', line 99 def read_concern @read_concern ||= [:read_concern] end |
#read_preference ⇒ Hash
Get the read preference on this collection.
123 124 125 |
# File 'lib/mongo/collection.rb', line 123 def read_preference @read_preference ||= [:read] || database.read_preference end |
#replace_one(filter, replacement, options = {}) ⇒ Result
Replaces a single document in the collection with the new document.
613 614 615 |
# File 'lib/mongo/collection.rb', line 613 def replace_one(filter, replacement, = {}) find(filter, ).replace_one(replacement, ) end |
#server_selector ⇒ Mongo::ServerSelector
Get the server selector on this collection.
111 112 113 |
# File 'lib/mongo/collection.rb', line 111 def server_selector @server_selector ||= ServerSelector.get(read_preference || database.server_selector) end |
#update_many(filter, update, options = {}) ⇒ Result
Update documents in the collection.
638 639 640 |
# File 'lib/mongo/collection.rb', line 638 def update_many(filter, update, = {}) find(filter, ).update_many(update, ) end |
#update_one(filter, update, options = {}) ⇒ Result
Update a single document in the collection.
663 664 665 |
# File 'lib/mongo/collection.rb', line 663 def update_one(filter, update, = {}) find(filter, ).update_one(update, ) end |
#watch(pipeline = [], options = {}) ⇒ ChangeStream
A change stream only allows ‘majority’ read concern.
This helper method is preferable to running a raw aggregation with a $changeStream stage, for the purpose of supporting resumability.
As of version 3.6 of the MongoDB server, a “$changeStream“ pipeline stage is supported in the aggregation framework. This stage allows users to request that notifications are sent for all changes to a particular collection.
337 338 339 |
# File 'lib/mongo/collection.rb', line 337 def watch(pipeline = [], = {}) View::ChangeStream.new(View.new(self, {}, ), pipeline, nil, ) end |
#with(new_options) ⇒ Mongo::Collection
Returns A new collection instance.
153 154 155 156 157 158 |
# File 'lib/mongo/collection.rb', line 153 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.
135 136 137 |
# File 'lib/mongo/collection.rb', line 135 def write_concern @write_concern ||= WriteConcern.get([:write] || database.write_concern) end |