vibe.db.mongo.collection

MongoCollection class

Types 9

Represents a single collection inside a MongoDB.

All methods take arbitrary types for Bson arguments. serializeToBson() is implicitly called on them before they are send to the database. The following example shows some possible ways to specify objects.

Fields
MongoClient m_client
string m_name
string m_fullPath
Methods
MongoDatabase database() @property @safeReturns: Root database to which this collection belongs.
string name() @property const @safeReturns: Name of this collection (excluding the database name).
void update(T, U)(T selector, U update, UpdateFlags flags = UpdateFlags.None)Performs an update operation on documents matching 'selector', updating them with 'update'.
void insert(T)(T document_or_documents, InsertFlags flags = InsertFlags.None)Inserts new documents into the collection.
InsertOneResult insertOne(T)(T document, InsertOneOptions options = InsertOneOptions.init)Inserts the provided document(s). If a document is missing an identifier, one is generated automatically by vibe.d.
InsertManyResult insertMany(T)(T[] documents, InsertManyOptions options = InsertManyOptions.init)ditto
DeleteResult deleteOne(T)(T filter, DeleteOptions options = DeleteOptions.init) @trustedDeletes at most one document matching the query `filter`. The returned result identifies how many documents have been deleted.
DeleteResult deleteMany(T)(T filter, DeleteOptions options = DeleteOptions.init) if (!is(T == DeleteOptions)) @safeDeletes all documents matching the query `filter`. The returned result identifies how many documents have been deleted.
DeleteResult deleteAll(DeleteOptions options = DeleteOptions.init) @safeDeletes all documents in the collection. The returned result identifies how many documents have been deleted.
DeleteResult deleteImpl(T)(T[] queries, DeleteOptions options = DeleteOptions.init, scope int[] limits = null) @safeImplementation helper. It's possible to set custom delete limits with this method, otherwise it's identical to `deleteOne` and `deleteMany`.
UpdateResult replaceOne(T, U)(T filter, U replacement, ReplaceOptions options) @safeReplaces at most single document within the collection based on the filter.
UpdateResult replaceOne(T, U)(T filter, U replacement, UpdateOptions options = UpdateOptions.init) @safeditto
UpdateResult updateOne(T, U)(T filter, U replacement, UpdateOptions options = UpdateOptions.init) @safeUpdates at most single document within the collection based on the filter.
UpdateResult updateMany(T, U)(T filter, U replacement, UpdateOptions options = UpdateOptions.init) @safeUpdates all matching document within the collection based on the filter.
UpdateResult updateImpl(T, U, O)(T[] queries, U[] documents, O[] perUpdateOptions, UpdateOptions options = UpdateOptions.init, bool mustBeDocument = false, bool mustBeModification = false) @safeImplementation helper. It's possible to set custom per-update object options with this method, otherwise it's identical to `replaceOne`, `updateOne` and `updateMany`.
MongoCursor!R find(R = Bson, T, U)(T query, U returnFieldSelector, QueryFlags flags, int num_skip = 0, int num_docs_per_chunk = 0)
MongoCursor!R find(R = Bson, T, U)(T query, U projection, FindOptions options = FindOptions.init) if (!is(U == FindOptions))Queries the collection for existing documents, limiting what fields are returned by the database. (called projection)
MongoCursor!R find(R = Bson, Q)(Q query, FindOptions options = FindOptions.init)Queries the collection for existing documents.
MongoCursor!R find(R = Bson)()Queries all documents of the collection.
MongoCursor!R find(R = Bson)(FindOptions options)ditto
deprecated("Use the overload taking `FindOptions` instead, this method breaks in MongoDB 5.1 and onwards. Note: using a `$query` / `query` member to override the query arguments is no longer supported in the new overload.") auto findOne(R = Bson, T, U)(T query, U returnFieldSelector, QueryFlags flags)
auto findOne(R = Bson, T, U)(T query, U projection, FindOptions options = FindOptions.init) if (!is(U == FindOptions))Queries the collection for existing documents.
auto findOne(R = Bson, T)(T query, FindOptions options = FindOptions.init)Queries the collection for existing documents.
void remove(T)(T selector, DeleteFlags flags = DeleteFlags.None)Removes documents from the collection.
void remove()()ditto
Bson findAndModify(T, U, V)(T query, U update, V returnFieldSelector)Combines a modify and find operation to a single atomic operation.
Bson findAndModify(T, U)(T query, U update)ditto
Bson findAndModifyExt(T, U, V)(T query, U update, V options)Combines a modify and find operation to a single atomic operation with generic options support.
ulong count(T)(T query)
private ulong countImpl(T)(T query)
ulong countDocuments(T)(T filter, CountOptions options = CountOptions.init)Returns the count of documents that match the query for a collection or view.
ulong estimatedDocumentCount(EstimatedDocumentCountOptions options = EstimatedDocumentCountOptions.init)Returns the count of all documents in a collection or view.
Bson aggregate(ARGS...)(ARGS pipeline) @safeCalculates aggregate values for the data in a collection.
MongoCursor!R aggregate(R = Bson, S = Bson)(S[] pipeline, AggregateOptions options) @safeditto
auto distinct(R = Bson, Q)(string fieldName, Q query, DistinctOptions options = DistinctOptions.init)Returns an input range of all unique values for a certain field for records matching the given query.
void ensureIndex(scope const(Tuple!(string, int))[] field_orders, IndexFlags flags = IndexFlags.none, Duration expire_time = 0.seconds) @safe
void ensureIndex(int[string] field_orders, IndexFlags flags = IndexFlags.none, ulong expireAfterSeconds = 0) @safe
void dropIndex(string name, DropIndexOptions options = DropIndexOptions.init) @safeDrops a single index from the collection by the index name.
void dropIndex(T)(T keys, IndexOptions indexOptions = IndexOptions.init, DropIndexOptions options = DropIndexOptions.init) if (!is(Unqual!T == IndexModel)) @safeditto
void dropIndex(const IndexModel keys, DropIndexOptions options = DropIndexOptions.init) @safeditto
void dropIndexes(DropIndexOptions options = DropIndexOptions.init) @safeDrops all indexes in the collection.
void dropIndexes(string[] names, DropIndexOptions options = DropIndexOptions.init) @safeUnofficial API extension, more efficient multi-index removal on MongoDB 4.2+
string createIndex(T)(T keys, IndexOptions indexOptions = IndexOptions.init, CreateIndexOptions options = CreateIndexOptions.init) if (!is(Unqual!T == IndexModel)) @safeConvenience method for creating a single index. Calls `createIndexes`
string createIndex(const IndexModel keys, CreateIndexOptions options = CreateIndexOptions.init) @safeditto
string[] createIndexes(scope const(IndexModel)[] models, CreateIndexesOptions options = CreateIndexesOptions.init, string file = __FILE__, size_t line = __LINE__) @safeBuilds one or more indexes in the collection.
MongoCursor!R listIndexes(R = Bson)() @safeReturns an array that holds a list of documents that identify and describe the existing indexes on the collection.
void drop() @safeRemoves a collection or view from the database. The method also removes any indexes associated with the dropped collection.
Constructors
this(MongoClient client, string fullPath)
this(ref MongoDatabase db, string name)

Specifies a level of isolation for read operations. For example, you can use read concern to only read data that has propagated to a majority of nodes in a replica set.

See Also

Fields
string levelThe level of the read concern.
Nested Templates
Level
Fields
Nullable!bool journalIf true, wait for the the write operation to get committed to the
Nullable!Bson wWhen an integer, specifies the number of nodes that should acknowledge the write and MUST be greater than or equal to 0.
Nullable!long wtimeoutMSIf provided, and the write concern is not satisfied within the specified timeout (in milliseconds), the server will return an error for the operation.
structCollation

Collation allows users to specify language-specific rules for string comparison, such as rules for letter-case and accent marks.

See Also

Fields
string localeThe ICU locale
Nullable!int strengthThe level of comparison to perform. Corresponds to ICU Comparison Levels.
Nullable!bool caseLevelFlag that determines whether to include case comparison at strength level 1 or 2.
Nullable!string caseFirstA flag that determines sort order of case differences during tertiary level comparisons.
Nullable!bool numericOrderingFlag that determines whether to compare numeric strings as numbers or as strings.
Nullable!Alternate alternateField that determines whether collation should consider whitespace and punctuation as base characters for purposes of comparison.
Nullable!MaxVariable maxVariableField that determines up to which characters are considered ignorable when `alternate: "shifted"`. Has no effect if `alternate: "non-ignorable"`
Nullable!bool backwardsFlag that determines whether strings with diacritics sort from back of the string, such as with some French dictionary ordering.
Nullable!bool normalizationFlag that determines whether to check if text require normalization and to perform normalization. Generally, majority of text does not require this normalization processing.
Nested Templates
Alternate
MaxVariable
Fields
Nullable!int batchSizeSpecifies the initial batch size for the cursor. Or null for server default value.

UDA to unset a nullable field if the server wire version doesn't at least match the given version. (inclusive)

Use with enforceWireVersionConstraints

Fields

UDA to warn when a nullable field is set and the server wire version matches the given version. (inclusive)

Use with enforceWireVersionConstraints

Fields

UDA to throw a MongoException when a nullable field is set and the server wire version doesn't match the version. (inclusive)

Use with enforceWireVersionConstraints

Fields

UDA to unset a nullable field if the server wire version is newer than the given version. (inclusive)

Use with enforceWireVersionConstraints

Fields

Functions 5

fnvoid enforceWireVersionConstraints(T)(ref T field, int serverVersion, string file = __FILE__, size_t line = __LINE__) @safeUnsets nullable fields not matching the server version as defined per UDAs.