DynFork::QCommons::General
General purpose query methods.
Instance methods
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 }
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
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.
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}})
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}}
)
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
Returns a variety of storage statistics for the collection. NOTE: For more details, please check the official documentation.