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
nameof the collection and thedbinstance. 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
trueif the collection has no documents, otherwisefalse.no setter -
isNotEmpty
→ Future<
bool> -
Checks if the collection is not empty.
Returns
trueif the collection contains documents, otherwisefalse.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
idshould 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
idshould be a valid MongoDB ObjectId string. Returnstrueif the deletion was successful, otherwisefalse. -
deleteAll(
) → Future< bool> -
Deletes all documents from the collection.
Returns
trueif the deletion was successful, otherwisefalse. 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
fieldandvalueexists in the collection. Returnstrueif the document exists, otherwisefalse. -
existId(
String idField) → Future< bool> -
Checks if a document with the given ID exists in the collection.
The
idFieldshould be a valid MongoDB ObjectId string. Returnstrueif the document exists, otherwisefalse. -
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
fieldandvalue, or by specifying a more complexfilterusing 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. Thefieldspecifies the field to be updated, andvalueis the new value to be assigned. Thefiltermap 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
idshould be a valid MongoDB ObjectId string. Thefieldspecifies the field to be updated, andvalueis 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
idshould be a valid MongoDB ObjectId string. Thefieldsmap contains the fields to be updated and their new values.
Operators
-
operator ==(
Object other) → bool -
The equality operator.
inherited