class Mongo::Database

Overview

A Database provides access to a MongoDB database.

database = client["database_name"]

Included Modules

Defined in:

cryomongo/database.cr

Instance Method Summary

Instance Method Detail

Get a newly allocated Mongo::Collection for the collection named name.


def aggregate(pipeline : Array, *, allow_disk_use : Bool | Nil = nil, batch_size : Int32 | Nil = nil, max_time_ms : Int64 | Nil = nil, bypass_document_validation : Bool | Nil = nil, read_concern : ReadConcern | Nil = nil, collation : Collation | Nil = nil, hint : String | H | Nil = nil, comment : String | Nil = nil, write_concern : WriteConcern | Nil = nil, session : Session::ClientSession | Nil = nil) : Mongo::Cursor | Nil forall H #

Runs an aggregation framework pipeline on the database for pipeline stages that do not require an underlying collection, such as $currentOp and $listLocalSessions.

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


def client : Mongo::Client #

The underlying MongoDB client.


def collection(collection : Collection::CollectionKey) : Mongo::Collection #

Get a newly allocated Mongo::Collection for the collection named name.


def command(operation, write_concern : WriteConcern | Nil = nil, read_concern : ReadConcern | Nil = nil, read_preference : ReadPreference | Nil = nil, session : Session::ClientSession | Nil = nil, **args, &) #

Execute a command on the server targeting the database.

Will automatically set the database arguments.

See: Mongo::Client.command


def command(operation, write_concern : WriteConcern | Nil = nil, read_concern : ReadConcern | Nil = nil, read_preference : ReadPreference | Nil = nil, session : Session::ClientSession | Nil = nil, **args) #

Execute a command on the server targeting the database.

Will automatically set the database arguments.

See: Mongo::Client.command


def grid_fs(bucket_name : String = "fs", *, chunk_size_bytes : Int32 = 255 * 1024, write_concern : WriteConcern | Nil = nil, read_concern : ReadConcern | Nil = nil, read_preference : ReadPreference | Nil = nil) : GridFS::Bucket #

Returns a Mongo::GridFS instance configured with the arguments provided.

NOTE for more details about GridFS, please check the official MongoDB manual.


def list_collections(*, filter = nil, name_only : Bool | Nil = nil, authorized_collections : Bool | Nil = nil, session : Session::ClientSession | Nil = nil) : Mongo::Cursor #

Retrieve information, i.e. the name and options, about the collections and views in a database.

Specifically, the command returns a document that contains information with which to create a cursor to the collection information.

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


def name : String #

The database name.


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 stats(*, scale : Int32 | Nil = nil) : BSON | Nil #

Returns a variety of storage statistics for the database.

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


def watch(pipeline : Array = [] of BSON, *, full_document : String | Nil = nil, resume_after : BSON | Nil = 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 : BSON | Nil = nil, read_concern : ReadConcern | Nil = nil, read_preference : ReadPreference | Nil = nil, session : Session::ClientSession | Nil = nil) : Mongo::ChangeStream::Cursor #

Returns a ChangeStream::Cursor watching all the database collection.

NOTE Excludes system collections.

client = Mongo::Client.new
database = client["db"]

spawn {
  cursor = database.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|
  database["collection"].insert_one({count: i})
end

sleep

NOTE for more details, please check the official manual.


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