Mongo::Collection
Inherits Mongo::WithReadPreference / Mongo::WithWriteConcern / Mongo::WithReadConcern / Reference / Object
A Collection provides access to a MongoDB collection.
collection = client["database_name"]["collection_name"]
Instance methods
Runs an aggregation framework pipeline.
NOTE: for more details, please check the official MongoDB documentation.
Create a Mongo::Bulk instance.
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.
See: Mongo::Client.command
Execute a command on the server targeting the collection.
Will automatically set the collection and database arguments.
See: Mongo::Client.command
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.
Read concern accessor.
Read concern accessor.
ReadPreference accessor.
ReadPreference accessor.
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
Write concern accessor.
Write concern accessor.