DBCollection class abstract

An abstract class representing a MongoDB collection with utility methods. The DBCollection class provides an abstraction over a MongoDB collection, offering various methods to interact with the database, such as checking for the existence of a document by its ID, updating fields, and more. This class is meant to be extended by other classes for more specific collection implementations.

Constructors

DBCollection({required String name, required Db db})
Constructor to initialize the name of the collection and the db instance. When initialized, the constructor checks if the collection exists in the database, and creates it if it does not already exist.

Properties

collection DbCollection
Provides direct access to the underlying MongoDB collection. The collection is retrieved using the db.collection(name) method.
no setter
db Db
The MongoDB database instance.
getter/setter pair
hashCode int
The hash code for this object.
no setterinherited
isEmpty Future<bool>
Checks if the collection is empty. Returns true if the collection has no documents, otherwise false.
no setter
isNotEmpty Future<bool>
Checks if the collection is not empty. Returns true if the collection contains documents, otherwise false.
no setter
name String
The name of the MongoDB collection.
getter/setter pair
runtimeType Type
A representation of the runtime type of the object.
no setterinherited

Methods

copy(String id) Future<void>
Creates a copy of a document by its ID and inserts it as a new document. The id should be a valid MongoDB ObjectId string. The copied document will have a new ObjectId assigned.
delete(String id) Future<bool>
Deletes a document from the collection by its ID. The id should be a valid MongoDB ObjectId string. Returns true if the deletion was successful, otherwise false.
deleteAll() Future<bool>
Deletes all documents from the collection. Returns true if the deletion was successful, otherwise false. You have to be careful when using this method, as it will delete all documents in the collection.
exist(String field, Object value) Future<bool>
Checks if a document with the specified field and value exists in the collection. Returns true if the document exists, otherwise false.
existId(String idField) Future<bool>
Checks if a document with the given ID exists in the collection. The idField should be a valid MongoDB ObjectId string. Returns true if the document exists, otherwise false.
getCount({String? field, Object? value, Map<String, Object?>? filter}) Future<int>
Retrieves the count of documents in the collection based on the specified filters. You can filter documents by providing a field and value, or by specifying a more complex filter using a map. If no filter is provided, the total count of documents in the collection is returned. Returns the count of matching documents.
noSuchMethod(Invocation invocation) → dynamic
Invoked when a nonexistent method or property is accessed.
inherited
toString() String
A string representation of this object.
inherited
updateAllForField({required String field, required Object? value, required Map<String, Object?>? filter}) Future<void>
Updates a specific field for all documents that match the given filter. The field specifies the field to be updated, and value is the new value to be assigned. The filter map is used to specify the matching condition.
updateField(String id, String field, Object? value) Future<void>
Updates a specific field of a document by its ID. The id should be a valid MongoDB ObjectId string. The field specifies the field to be updated, and value is the new value to be assigned.
updateFields(String id, Map<String, dynamic> fields) Future<void>
Updates multiple fields of a document by its ID. The id should be a valid MongoDB ObjectId string. The fields map contains the fields to be updated and their new values.

Operators

operator ==(Object other) bool
The equality operator.
inherited