where method

BulkUpdate<T> where(
  1. Object field,
  2. {Object? isEqualTo,
  3. Object? isNotEqualTo,
  4. Object? isLessThan,
  5. Object? isLessThanOrEqualTo,
  6. Object? isGreaterThan,
  7. Object? isGreaterThanOrEqualTo,
  8. Object? arrayContains,
  9. Iterable<Object?>? arrayContainsAny,
  10. Iterable<Object?>? whereIn,
  11. Iterable<Object?>? whereNotIn,
  12. bool? isNull}
)

Adds a condition to filter documents for the bulk update operation.

This method allows chaining multiple conditions to specify which documents should be updated. Only one comparison operator can be specified per call.

Parameters:

  • field: The field to filter on. Can be a String field name, FieldPath, or a Filter object for complex conditions.
  • isEqualTo: Filters documents where the field equals this value.
  • isNotEqualTo: Filters documents where the field does not equal this value.
  • isLessThan: Filters documents where the field is less than this value.
  • isLessThanOrEqualTo: Filters documents where the field is less than or equal to this value.
  • isGreaterThan: Filters documents where the field is greater than this value.
  • isGreaterThanOrEqualTo: Filters documents where the field is greater than or equal to this value.
  • arrayContains: Filters documents where the array field contains this exact value.
  • arrayContainsAny: Filters documents where the array field contains any of the values in this iterable.
  • whereIn: Filters documents where the field is equal to any value in this iterable.
  • whereNotIn: Filters documents where the field is not equal to any value in this iterable.
  • isNull: If true, filters documents where the field is null.

Returns: A BulkUpdate instance for chaining additional conditions.

Example:

bulkUpdate
  .where('age', isGreaterThan: 18)
  .where('status', isEqualTo: 'active')
  .where('tags', arrayContains: 'important');

Implementation

BulkUpdate<T> where(Object field,
    {Object? isEqualTo,
    Object? isNotEqualTo,
    Object? isLessThan,
    Object? isLessThanOrEqualTo,
    Object? isGreaterThan,
    Object? isGreaterThanOrEqualTo,
    Object? arrayContains,
    Iterable<Object?>? arrayContainsAny,
    Iterable<Object?>? whereIn,
    Iterable<Object?>? whereNotIn,
    bool? isNull
    }) {
  if (field is Filter) {
    var filters = getAllWhere(field);
    dynamic res = this;
    for (int i = 0;i<filters.length;i++) {
      res._conditions.add(filters[i]);
    }
  }
  Map<String, dynamic> conds = {
    "isEqualTo": isEqualTo,
    "isNotEqualTo": isNotEqualTo,
    "isLessThan": isLessThan,
    "isLessThanOrEqualTo": isLessThanOrEqualTo,
    "isGreaterThan": isGreaterThan,
    "isGreaterThanOrEqualTo": isGreaterThanOrEqualTo,
    "arrayContains": arrayContains,
    "arrayContainsAny": arrayContainsAny,
    "whereIn": whereIn,
    "whereNotIn": whereNotIn,
    "isNull": isNull
  };

  if (field is FieldPath) {
    field = field.getPath();
  }

  BulkUpdate<T> newQuery = this;

  conds.forEach((key, value) {
      if (value != null && value != '') {
        newQuery._ops.add(key);
        if (key == "arrayContains") {
          for (int i=0;i<newQuery._ops.length;i++) {
            if (newQuery._ops[i] == "arrayContainsAny") {
              throw Utils.oracledbErrorHandler(
                400, OracledbErrorMessages.invalidArrayContainsCombination, null);
            }
          }
        }
        if (key == "arrayContainsAny") {
          for (int i=0;i<newQuery._ops.length;i++) {
            if (newQuery._ops[i] == "arrayContains") {
              throw Utils.oracledbErrorHandler(
                400, OracledbErrorMessages.invalidArrayContainsCombination, null);
            }
            }
          }
        if (("${whereOperators[key]}" == "in" || "${whereOperators[key]}" == "not in") && value is! List) {
          value = [value];
        }
        newQuery._conditions
            .add({"field": "$field", "op": "${whereOperators[key]}", "value": value});
      }
    }
  );

  return newQuery;
}