DBIndex class

Configuration class for MongoDB database indexes. This class encapsulates all the configuration options needed to create a MongoDB index. It provides a convenient way to define index properties that will be used when creating indexes on collection fields. MongoDB indexes improve query performance and can enforce constraints like uniqueness. This class supports all major index options including:

  • Single field and compound indexes
  • Unique constraints
  • Sparse indexes (ignore null values)
  • Partial indexes with filter expressions
  • Background index creation
  • Custom collation for text sorting Example:
var emailIndex = DBIndex(
  key: 'email',
  unique: true,
  sparse: true,
);
var compoundIndex = DBIndex(
  keys: {'category': 1, 'createdAt': -1},
  name: 'category_date_idx',
);

Constructors

DBIndex({String? key, Map<String, dynamic>? keys, bool? unique, bool? sparse, bool? background, bool? dropDups, Map<String, dynamic>? partialFilterExpression, String? name, bool? modernReply, Map? collation})
Creates a new database index configuration. Parameters:

Properties

background bool?
Whether to build the index in the background. Background index builds don't block database operations but may take longer to complete.
getter/setter pair
collation Map?
Collation specification for the index. Collation allows you to specify language-specific rules for string comparison, such as case sensitivity and accent sensitivity.
getter/setter pair
dropDups bool?
Whether to drop duplicate documents during index creation. This option is deprecated in newer MongoDB versions.
getter/setter pair
hashCode int
The hash code for this object.
no setterinherited
key String?
Single field name to index (alternative to keys). Use this for simple single-field indexes. Cannot be used together with the keys parameter.
getter/setter pair
keys Map<String, dynamic>?
Map defining compound index fields and their sort order. Keys are field names, values are:
getter/setter pair
modernReply bool?
Whether to use modern reply format for index creation. This affects the response format from the index creation operation.
getter/setter pair
name String?
Custom name for the index. If not provided, MongoDB generates a name based on the field names and sort order.
getter/setter pair
partialFilterExpression Map<String, dynamic>?
Filter expression for partial indexes. Partial indexes only index documents that meet the specified filter criteria, reducing index size and improving performance.
getter/setter pair
runtimeType Type
A representation of the runtime type of the object.
no setterinherited
sparse bool?
Whether to create a sparse index. Sparse indexes only contain entries for documents that have the indexed field, ignoring documents where the field is null or missing.
getter/setter pair
unique bool?
Whether the index should enforce uniqueness constraint. When true, MongoDB will reject documents that would create duplicate values for the indexed field(s).
getter/setter pair

Methods

noSuchMethod(Invocation invocation) → dynamic
Invoked when a nonexistent method or property is accessed.
inherited
toString() String
A string representation of this object.
inherited

Operators

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