class

Mongo::Database

Inherits Mongo::WithReadPreference / Mongo::WithWriteConcern / Mongo::WithReadConcern / Reference / Object

A Database provides access to a MongoDB database.

database = client["database_name"]

Instance methods

[](collection : Collection::CollectionKey) : Mongo::Collection

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

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

Source
client

The underlying MongoDB client.

Source
collection(collection : Collection::CollectionKey) : Mongo::Collection

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

Source
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

Source
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

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

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

Source
name

The database name.

Source
read_concern

Read concern accessor.

See: the official documentation

read_concern=(read_concern : ReadConcern | Nil)

Read concern accessor.

See: the official documentation

read_preference

ReadPreference accessor.

See: the official documentation.

read_preference=(read_preference : ReadPreference | Nil)

ReadPreference accessor.

See: the official documentation.

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.

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

Source
write_concern

Write concern accessor.

See: the official documentation

write_concern=(write_concern : WriteConcern | Nil)

Write concern accessor.

See: the official documentation