class Mongo::Collection
- Mongo::Collection
- Reference
- Object
Overview
A Collection
provides access to a MongoDB collection.
collection = client["database_name"]["collection_name"]
Included Modules
- Mongo::WithReadConcern
- Mongo::WithReadPreference
- Mongo::WithWriteConcern
Defined in:
cryomongo/collection.crInstance Method Summary
-
#aggregate(pipeline : Array, *, allow_disk_use : Bool? = nil, batch_size : Int32? = nil, max_time_ms : Int64? = nil, bypass_document_validation : Bool? = nil, collation : Collation? = nil, hint : String | H? = nil, comment : String? = nil, read_concern : ReadConcern? = nil, write_concern : WriteConcern? = nil, read_preference : ReadPreference? = nil, session : Session::ClientSession? = nil) : Mongo::Cursor? forall H
Runs an aggregation framework pipeline.
-
#bulk(ordered : Bool = true, session : Session::ClientSession? = nil)
Create a
Mongo::Bulk
instance. -
#bulk_write(requests : Array(Bulk::WriteModel), *, ordered : Bool, bypass_document_validation : Bool? = nil, session : Session::ClientSession? = nil) : Bulk::WriteResult
Executes multiple write operations.
-
#command(operation, write_concern : WriteConcern? = nil, read_concern : ReadConcern? = nil, read_preference : ReadPreference? = nil, session : Session::ClientSession? = nil, **args, &)
Execute a command on the server targeting the collection.
-
#command(operation, write_concern : WriteConcern? = nil, read_concern : ReadConcern? = nil, read_preference : ReadPreference? = nil, session : Session::ClientSession? = nil, **args)
Execute a command on the server targeting the collection.
-
#count_documents(filter = BSON.new, *, skip : Int32? = nil, limit : Int32? = nil, collation : Collation? = nil, hint : String | H? = nil, max_time_ms : Int64? = nil, read_preference : ReadPreference? = nil, session : Session::ClientSession? = nil) : Int32 forall H
Count the number of documents in a collection that match the given filter.
-
#create_index(keys, *, options = NamedTuple.new, commit_quorum : Int32 | String? = nil, max_time_ms : Int64? = nil, write_concern : WriteConcern? = nil, session : Session::ClientSession? = nil) : Commands::CreateIndexes::Result?
This is a convenience method for creating a single index.
-
#create_indexes(models : Array, *, commit_quorum : Int32 | String? = nil, max_time_ms : Int64? = nil, write_concern : WriteConcern? = nil, session : Session::ClientSession? = nil) : Commands::CreateIndexes::Result?
Creates multiple indexes in the collection.
-
#database : Mongo::Database
The parent database.
-
#delete_many(filter, *, collation : Collation? = nil, hint : String | H? = nil, ordered : Bool? = nil, write_concern : WriteConcern? = nil, session : Session::ClientSession? = nil) : Commands::Common::DeleteResult? forall H
Deletes multiple documents.
-
#delete_one(filter, *, collation : Collation? = nil, hint : String | H? = nil, ordered : Bool? = nil, write_concern : WriteConcern? = nil, session : Session::ClientSession? = nil) : Commands::Common::DeleteResult? forall H
Deletes one document.
-
#distinct(key : String, *, filter = nil, read_concern : ReadConcern? = nil, collation : Collation? = nil, read_preference : ReadPreference? = nil, session : Session::ClientSession? = nil) : Array
Finds the distinct values for a specified field across a single collection.
-
#drop_index(name : String, *, max_time_ms : Int64? = nil, write_concern : WriteConcern? = nil, session : Session::ClientSession? = nil) : Commands::Common::BaseResult?
Drops a single index from the collection by the index name.
-
#drop_indexes(*, max_time_ms : Int64? = nil, write_concern : WriteConcern? = nil, session : Session::ClientSession? = nil) : Commands::Common::BaseResult?
Drops all indexes in the collection.
-
#estimated_document_count(*, max_time_ms : Int64? = nil, read_preference : ReadPreference? = nil, session : Session::ClientSession? = nil) : Int32
Gets an estimate of the count of documents in a collection using collection metadata.
-
#find(filter = BSON.new, *, sort = nil, projection = nil, hint : String | H? = nil, skip : Int32? = nil, limit : Int32? = nil, batch_size : Int32? = nil, single_batch : Bool? = nil, comment : String? = nil, max_time_ms : Int64? = nil, read_concern : ReadConcern? = nil, max = nil, min = nil, return_key : Bool? = nil, show_record_id : Bool? = nil, tailable : Bool? = nil, oplog_replay : Bool? = nil, no_cursor_timeout : Bool? = nil, await_data : Bool? = nil, allow_partial_results : Bool? = nil, allow_disk_use : Bool? = nil, collation : Collation? = nil, read_preference : ReadPreference? = nil, session : Session::ClientSession? = nil) : Mongo::Cursor forall H
Finds the documents matching the model.
-
#find_one(filter = BSON.new, *, sort = nil, projection = nil, hint : String | H? = nil, skip : Int32? = nil, comment : String? = nil, max_time_ms : Int64? = nil, read_concern : ReadConcern? = nil, max = nil, min = nil, return_key : Bool? = nil, show_record_id : Bool? = nil, oplog_replay : Bool? = nil, no_cursor_timeout : Bool? = nil, allow_partial_results : Bool? = nil, collation : Collation? = nil, read_preference : ReadPreference? = nil, session : Session::ClientSession? = nil) : BSON? forall H
Finds the document matching the model.
-
#find_one_and_delete(filter, *, sort = nil, fields = nil, bypass_document_validation : Bool? = nil, write_concern : WriteConcern? = nil, collation : Collation? = nil, hint : String | H? = nil, max_time_ms : Int64? = nil, session : Session::ClientSession? = nil) : BSON? forall H
Finds a single document and deletes it, returning the original.
-
#find_one_and_replace(filter, replacement, *, sort = nil, new : Bool? = nil, fields = nil, upsert : Bool? = nil, bypass_document_validation : Bool? = nil, write_concern : WriteConcern? = nil, collation : Collation? = nil, array_filters = nil, hint : String | H? = nil, max_time_ms : Int64? = nil, session : Session::ClientSession? = nil) : BSON? forall H
Finds a single document and replaces it, returning either the original or the replaced document.
-
#find_one_and_update(filter, update, *, sort = nil, new : Bool? = nil, fields = nil, upsert : Bool? = nil, bypass_document_validation : Bool? = nil, write_concern : WriteConcern? = nil, collation : Collation? = nil, array_filters = nil, hint : String | H? = nil, max_time_ms : Int64? = nil, session : Session::ClientSession? = nil) : BSON? forall H
Finds a single document and updates it, returning either the original or the updated document.
-
#insert_many(documents : Array, *, ordered : Bool? = nil, write_concern : WriteConcern? = nil, bypass_document_validation : Bool? = nil, session : Session::ClientSession? = nil) : Commands::Common::InsertResult?
Inserts the provided document.
-
#insert_one(document, *, write_concern : WriteConcern? = nil, bypass_document_validation : Bool? = nil, session : Session::ClientSession? = nil) : Commands::Common::InsertResult?
Inserts the provided document.
-
#list_indexes(session : Session::ClientSession? = nil) : Mongo::Cursor?
Gets index information for all indexes in the collection.
-
#name : CollectionKey
The collection name.
-
#read_concern : ReadConcern?
Read concern accessor.
-
#read_concern=(read_concern : ReadConcern?)
Read concern accessor.
-
#read_preference : ReadPreference?
ReadPreference accessor.
-
#read_preference=(read_preference : ReadPreference?)
ReadPreference accessor.
-
#replace_one(filter, replacement, *, upsert : Bool = false, collation : Collation? = nil, hint : String | H? = nil, ordered : Bool? = nil, write_concern : WriteConcern? = nil, bypass_document_validation : Bool? = nil, session : Session::ClientSession? = nil) : Commands::Common::UpdateResult? forall H
Replaces a single document.
-
#stats(*, scale : Int32? = nil, session : Session::ClientSession? = nil) : BSON?
Returns a variety of storage statistics for the collection.
-
#update_many(filter, update, *, upsert : Bool = false, array_filters = nil, collation : Collation? = nil, hint : String | H? = nil, ordered : Bool? = nil, write_concern : WriteConcern? = nil, bypass_document_validation : Bool? = nil, session : Session::ClientSession? = nil) : Commands::Common::UpdateResult? forall H
Updates multiple documents.
-
#update_one(filter, update, *, upsert : Bool = false, array_filters = nil, collation : Collation? = nil, hint : String | H? = nil, ordered : Bool? = nil, write_concern : WriteConcern? = nil, bypass_document_validation : Bool? = nil, session : Session::ClientSession? = nil) : Commands::Common::UpdateResult? forall H
Updates one document.
-
#watch(pipeline : Array = [] of BSON, *, full_document : String? = nil, start_at_operation_time : Time? = nil, resume_after : BSON? = nil, start_after : BSON? = nil, max_await_time_ms : Int64? = nil, batch_size : Int32? = nil, collation : Collation? = nil, read_concern : ReadConcern? = nil, read_preference : ReadPreference? = nil, session : Session::ClientSession? = nil) : Mongo::ChangeStream::Cursor
Returns a
ChangeStream::Cursor
watching a specific collection. -
#with_session(**args, &)
Initialize a session that has the same lifetime as the block.
-
#write_concern : WriteConcern?
Write concern accessor.
-
#write_concern=(write_concern : WriteConcern?)
Write concern accessor.
Instance Method Detail
Runs an aggregation framework pipeline.
NOTE for more details, please check the official MongoDB documentation.
Executes multiple write operations.
An error will be raised if the requests parameter is empty.
NOTE for more details, please check the official specifications document.
Execute a command on the server targeting the collection.
Will automatically set the collection and database arguments.
Execute a command on the server targeting the collection.
Will automatically set the collection and database arguments.
Count the number of documents in a collection that match the given filter.
Note that an empty filter will force a scan of the entire collection.
For a fast count of the total documents in a collection see #estimated_document_count
.
This is a convenience method for creating a single index.
See: #create_indexes
Creates multiple indexes in the collection.
NOTE for more details, please check the official documentation.
Deletes multiple documents.
NOTE for more details, please check the official documentation.
Deletes one document.
NOTE for more details, please check the official documentation.
Finds the distinct values for a specified field across a single collection.
NOTE the results are backed by the "values" array in the distinct command's result document. This differs from aggregate and find, where results are backed by a cursor.
NOTE for more details, please check the official MongoDB documentation.
Drops a single index from the collection by the index name.
See: #drop_indexes
Drops all indexes in the collection.
NOTE for more details, please check the official documentation.
Gets an estimate of the count of documents in a collection using collection metadata.
Finds the documents matching the model.
NOTE for more details, please check the official MongoDB documentation.
NOTE for an overview of read operations, check the official manual.
Finds the document matching the model.
NOTE for more details, please check the official MongoDB documentation.
Finds a single document and deletes it, returning the original. The document to return may be nil.
NOTE for more details, please check the official documentation.
Finds a single document and replaces it, returning either the original or the replaced document. The document to return may be nil.
NOTE for more details, please check the official documentation.
Finds a single document and updates it, returning either the original or the updated document. The document to return may be nil.
NOTE for more details, please check the official documentation.
Inserts the provided document. If any documents are missing an identifier, they will be generated.
NOTE for more details, please check the official documentation.
Inserts the provided document. If the document is missing an identifier, it will be generated.
NOTE for more details, please check the official documentation.
Gets index information for all indexes in the collection.
NOTE for more details, please check the official documentation.
Replaces a single document.
NOTE for more details, please check the official documentation.
Returns a variety of storage statistics for the collection.
NOTE for more details, please check the official MongoDB documentation.
Updates multiple documents.
NOTE for more details, please check the official documentation.
Updates one document.
NOTE for more details, please check the official documentation.
Returns a ChangeStream::Cursor
watching a specific collection.
client = Mongo::Client.new
collection = client["db"]["coll"]
spawn {
cursor = collection.watch(
[
{"$match": {"operationType": "insert"}},
],
max_await_time_ms: 10000
)
# cursor.of(BSON) converts to the Mongo::ChangeStream::Document(BSON) type.
cursor.of(BSON).each { |doc|
puts doc.to_bson.to_json
}
}
100.times do |i|
collection.insert_one({count: i})
end
sleep
Initialize a session that has the same lifetime as the block.
- First block argument is a reflection of the Collection instance with the session method argument already provided.
- Second block argument is the ClientSession.
client = Mongo::Client.new
collection = client["db"]["coll"]
collection.with_session(causal_consistency: true) do |collection, session|
5.times { |idx|
# No need to provide: `session: session`.
collection.insert_one({number: idx})
collection.find_one({number: idx})
}
end