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, batch_size : Int32? = nil, max_time_ms : Int64? = nil, bypass_document_validation : Bool? = nil, read_concern : ReadConcern? = nil, collation : Collation? = nil, hint : String | H? = nil, comment : String? = nil, write_concern : WriteConcern? = nil, session : Session::ClientSession? = nil) : Mongo::Cursor? 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, read_concern : ReadConcern? = nil, read_preference : ReadPreference? = nil, session : Session::ClientSession? = 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, read_concern : ReadConcern? = nil, read_preference : ReadPreference? = nil, session : Session::ClientSession? = 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, read_concern : ReadConcern? = nil, read_preference : ReadPreference? = 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, authorized_collections : Bool? = nil, session : Session::ClientSession? = 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? #

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

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, resume_after : BSON? = nil, max_await_time_ms : Int64? = nil, batch_size : Int32? = nil, collation : Collation? = nil, start_at_operation_time : Time? = nil, start_after : BSON? = nil, read_concern : ReadConcern? = nil, read_preference : ReadPreference? = nil, session : Session::ClientSession? = 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? #

Write concern accessor.

See: the official documentation


def write_concern=(write_concern : WriteConcern?) #

Write concern accessor.

See: the official documentation