class Mongo::Collection

Overview

A Collection provides access to a MongoDB collection.

collection = client["database_name"]["collection_name"]

Included Modules

Defined in:

cryomongo/collection.cr

Instance Method Summary

Instance Method Detail

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

NOTE for more details, please check the official MongoDB documentation.


def bulk(ordered : Bool = true, session : Session::ClientSession? = nil) #

Create a Mongo::Bulk instance.


def bulk_write(requests : Array(Bulk::WriteModel), *, ordered : Bool, bypass_document_validation : Bool? = nil, session : Session::ClientSession? = nil) : Bulk::WriteResult #

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.


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

Will automatically set the collection and database arguments.

See: Mongo::Client.command


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

Will automatically set the collection and database arguments.

See: Mongo::Client.command


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

See: the specification document.


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

See: #create_indexes


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

NOTE for more details, please check the official documentation.


def database : Mongo::Database #

The parent database.


def 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 #

def 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 #

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

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.


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

See: #drop_indexes


def drop_indexes(*, max_time_ms : Int64? = nil, write_concern : WriteConcern? = nil, session : Session::ClientSession? = nil) : Commands::Common::BaseResult? #

Drops all indexes in the collection.

NOTE for more details, please check the official documentation.


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

See: the specification document.


def 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 #

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

NOTE for more details, please check the official MongoDB documentation.


def 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. The document to return may be nil.

NOTE for more details, please check the official documentation.


def 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. The document to return may be nil.

NOTE for more details, please check the official documentation.


def 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. The document to return may be nil.

NOTE for more details, please check the official documentation.


def 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. If any documents are missing an identifier, they will be generated.

NOTE for more details, please check the official documentation.


def insert_one(document, *, write_concern : WriteConcern? = nil, bypass_document_validation : Bool? = nil, session : Session::ClientSession? = nil) : Commands::Common::InsertResult? #

Inserts the provided document. If the document is missing an identifier, it will be generated.

NOTE for more details, please check the official documentation.


def list_indexes(session : Session::ClientSession? = nil) : Mongo::Cursor? #

Gets index information for all indexes in the collection.

NOTE for more details, please check the official documentation.


def name : CollectionKey #

The collection name.


def read_concern : ReadConcern? #

Read concern accessor.

See: the official documentation


def read_concern=(read_concern : ReadConcern?) #

Read concern accessor.

See: the official documentation


def read_preference : ReadPreference? #

ReadPreference accessor.

See: the official documentation.


def read_preference=(read_preference : ReadPreference?) #

ReadPreference accessor.

See: the official documentation.


def 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 #

def stats(*, scale : Int32? = nil, session : Session::ClientSession? = nil) : BSON? #

Returns a variety of storage statistics for the collection.

NOTE for more details, please check the official MongoDB documentation.


def 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 #

def 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 #

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

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

NOTE for more details, please check the official manual.


def with_session(**args, &) #

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

def write_concern : WriteConcern? #

Write concern accessor.

See: the official documentation


def write_concern=(write_concern : WriteConcern?) #

Write concern accessor.

See: the official documentation