class Mongo::Client

Overview

The client which provides access to a MongoDB server, replica set, or sharded cluster.

It maintains management of underlying sockets and routing to individual nodes.

Included Modules

Defined in:

cryomongo/client.cr

Constant Summary

MAX_WIRE_VERSION = 8

The maximum wire protocol version supported by this driver.

MIN_WIRE_VERSION = 6

The mininum wire protocol version supported by this driver.

Constructors

Instance Method Summary

Constructor Detail

def self.new(connection_string : String = "mongodb://localhost:27017") #

Create a mongodb client instance from a mongodb URL.

require "cryomongo"

client = Mongo::Client.new "mongodb://127.0.0.1/?appname=client-example"

[View source]

Instance Method Detail

def [](name : String) : Database #

Get a newly allocated Mongo::Database for the database named name.


[View source]
def close #

Frees all the resources associated with a client.


[View source]
def command(command cmd, write_concern : WriteConcern | Nil = nil, read_concern : ReadConcern | Nil = nil, read_preference : ReadPreference | Nil = nil, server_description : SDAM::ServerDescription | Nil = nil, ignore_errors = false, **args) #

Execute a command on the server.

# First argument is the `Mongo::Commands`.
client.command(Mongo::Commands::DropDatabase, database: "database_name")

[View source]
def database(name : String) : Database #

Get a newly allocated Mongo::Database for the database named name.


[View source]
def list_databases(*, filter = nil, name_only : Bool | Nil = nil, authorized_databases : Bool | Nil = nil) : Commands::ListDatabases::Result #

Provides a list of all existing databases along with basic statistics about them.

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


[View source]
def options : Options #

The set of driver options.


[View source]
def read_concern : ReadConcern | Nil #

Read concern accessor.

See: the official documentation


def read_concern=(read_concern : ReadConcern | Nil) #

Read concern accessor.

See: the official documentation


def read_preference : ReadPreference | Nil #

ReadPreference accessor.

See: the official documentation.


def read_preference=(read_preference : ReadPreference | Nil) #

ReadPreference accessor.

See: the official documentation.


def status(*, repl : Int32 | Nil = nil, metrics : Int32 | Nil = nil, locks : Int32 | Nil = nil, mirrored_reads : Int32 | Nil = nil, latch_analysis : Int32 | Nil = nil) : BSON | Nil #

Returns a document that provides an overview of the database’s state.

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


[View source]
def top : BSON | Nil #

An administrative command that returns usage statistics for each collection.

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


[View source]
def watch(pipeline : Array = [] of BSON, *, full_document : String | Nil = nil, resume_after = nil, max_await_time_ms : Int64 | Nil = nil, batch_size : Int32 | Nil = nil, collation : Collation | Nil = nil, start_at_operation_time : Time | Nil = nil, start_after = nil, read_concern : ReadConcern | Nil = nil, read_preference : ReadPreference | Nil = nil) : Mongo::ChangeStream::Cursor #

Allows a client to observe all changes in a cluster.

Returns a change stream on all collections in all databases in a cluster.

NOTE Excludes system collections.


[View source]
def write_concern : WriteConcern | Nil #

Write concern accessor.

See: the official documentation


def write_concern=(write_concern : WriteConcern | Nil) #

Write concern accessor.

See: the official documentation