class Mongo::Database
- Mongo::Database
- Reference
- Object
Overview
A Database
provides access to a MongoDB database.
database = client["database_name"]
Included Modules
- Mongo::WithReadConcern
- Mongo::WithReadPreference
- Mongo::WithWriteConcern
Defined in:
cryomongo/database.crInstance Method Summary
-
#[](collection : Collection::CollectionKey) : Mongo::Collection
Get a newly allocated
Mongo::Collection
for the collection named name. -
#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.
-
#client : Mongo::Client
The underlying MongoDB client.
-
#collection(collection : Collection::CollectionKey) : Mongo::Collection
Get a newly allocated
Mongo::Collection
for the collection named name. -
#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.
-
#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.
-
#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. -
#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.
-
#name : String
The database name.
-
#read_concern : ReadConcern | Nil
Read concern accessor.
-
#read_concern=(read_concern : ReadConcern | Nil)
Read concern accessor.
-
#read_preference : ReadPreference | Nil
ReadPreference accessor.
-
#read_preference=(read_preference : ReadPreference | Nil)
ReadPreference accessor.
-
#stats(*, scale : Int32 | Nil = nil) : BSON | Nil
Returns a variety of storage statistics for the database.
-
#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. -
#write_concern : WriteConcern | Nil
Write concern accessor.
-
#write_concern=(write_concern : WriteConcern | Nil)
Write concern accessor.
Instance Method Detail
Get a newly allocated Mongo::Collection
for the collection named name.
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.
Get a newly allocated Mongo::Collection
for the collection named name.
Execute a command on the server targeting the database.
Will automatically set the database arguments.
Execute a command on the server targeting the database.
Will automatically set the database arguments.
Returns a Mongo::GridFS
instance configured with the arguments provided.
NOTE for more details about GridFS, please check the official MongoDB manual.
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.
Returns a variety of storage statistics for the database.
NOTE for more details, please check the official MongoDB documentation.
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