module DynFork::QCommons::General

Overview

General purpose query methods.

Extended Modules

Direct including types

Defined in:

dynfork/commons/general.cr

Instance Method Summary

Instance Method Detail

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, collation : Mongo::Collation | Nil = nil, hint : String | Hash | NamedTuple | Nil = nil, comment : String | Nil = nil, read_concern : Mongo::ReadConcern | Nil = nil, write_concern : Mongo::WriteConcern | Nil = nil, read_preference : Mongo::ReadPreference | Nil = nil, session : Mongo::Session::ClientSession | Nil = nil) : Mongo::Cursor | Nil #

Runs an aggregation framework pipeline.

NOTE For more details, please check the official documentation.

NOTE For more details, please check the cryomongo documentation.

Example:

# Perform an aggregation pipeline query
cursor = ModelName.aggregate([
  {"$match": { status: "available" }}
  {"$limit": 5},
])
cursor.try &.each { |bson| puts bson.to_json }

[View source]
def bulk(ordered : Bool = true, session : Mongo::Session::ClientSession | Nil = nil) : Mongo::Bulk #

Create a Mongo::Bulk instance.

NOTE A bulk operations builder.

NOTE For more details, please check the cryomongo documentation.

Example:

bulk = ModelName.bulk(ordered: true)
# Then, operations can be added…
500.times { |idx|
  bulk.insert_one({number: idx})
  bulk.delete_many({number: {"$lt": 450}})
}
# …and they will be performed once the bulk gets executed.
bulk_result = bulk.execute(write_concern: Mongo::WriteConcern.new(w: 1))
pp bulk_result

[View source]
def bulk_write(requests : Array(Mongo::Bulk::WriteModel), *, ordered : Bool, bypass_document_validation : Bool | Nil = nil, session : Mongo::Session::ClientSession | Nil = nil) : Mongo::Bulk::WriteResult #

Executes multiple write operations. An error will be raised if the requests parameter is empty.

NOTE For more details, please check the official documentation.

NOTE For more details, please check the cryomongo documentation.


[View source]
def collection_name : Mongo::Collection::CollectionKey #

Get collection name.


[View source]
def count_documents(filter = BSON.new, *, skip : Int32 | Nil = nil, limit : Int32 | Nil = nil, collation : Mongo::Collation | Nil = nil, hint : String | Hash | NamedTuple | Nil = nil, max_time_ms : Int64 | Nil = nil, read_preference : Mongo::ReadPreference | Nil = nil, session : Mongo::Session::ClientSession | Nil = nil) : Int32 #

Count the number of documents in a collection that match the given filter.
Note that an empty filter will force a scan of the entire collection.

NOTE For a fast count of the total documents in a collection see estimated_document_count.

NOTE For more details, please check the cryomongo documentation.

Example:

# Documents count
counter = ModelName.count_documents({age: {"$lt": 18}})

[View source]
def distinct(key : String, *, filter = nil, read_concern : Mongo::ReadConcern | Nil = nil, collation : Mongo::Collation | Nil = nil, read_preference : Mongo::ReadPreference | Nil = nil, session : Mongo::Session::ClientSession | Nil = nil) : Array #

Finds the distinct values for a specified field across a single collection.
Returns an array of unique values for specified field of collection.

NOTE the results are backed by the "values" array in the distinct command's result document. This differs from aggregate and find, where results are backed by a cursor.

NOTE For more details, please check the official documentation.

NOTE For more details, please check the cryomongo documentation.

Example:

# Distinct collection values
values = ModelName.distinct(
  key: "field",
  filter: {age: {"$gt": 18}}
)

[View source]
def estimated_document_count(*, max_time_ms : Int64 | Nil = nil, read_preference : Mongo::ReadPreference | Nil = nil, session : Mongo::Session::ClientSession | Nil = nil) : Int32 #

Gets an estimate of the count of documents in a collection using collection metadata.

NOTE For more details, please check the official documentation.

NOTE For more details, please check the cryomongo documentation.

Example:

# Estimated count
counter = ModelName.estimated_document_count

[View source]
def stats(*, scale : Int32 | Nil = nil, session : Mongo::Session::ClientSession | Nil = nil) : BSON | Nil #

Returns a variety of storage statistics for the collection.

NOTE For more details, please check the official documentation.


[View source]