updateOne method

Future<List> updateOne({
  1. required dynamic filter,
  2. required UpdateOperator update,
})

Update a single document in the collection according to the specified arguments.

Implementation

Future<List> updateOne(
    {required filter, required UpdateOperator update}) async {
  assert(filter is Map<String, dynamic> || filter is LogicalQueryOperator);

  if (filter is Map<String, dynamic>) {
    // convert 'QuerySelector' into map, too
    filter.forEach((dynamic key, value) {
      if (value is QueryOperator) {
        filter[key] = value.values;
      }
    });
  }
  if (filter is LogicalQueryOperator) {
    filter = filter.values;
  }

  var updateValues = update.values.map((key, value) {
    if (value is Map<String, dynamic>) {
      var valueNew = <String, dynamic>{};
      valueNew.addAll(value);

      valueNew.forEach((key2, value2) {
        if (value2 is ArrayModifier) {
          valueNew[key2] = value2.values;
        } else if (value2 is QueryOperator) {
          valueNew[key2] = value2.values;
        }
      });

      return MapEntry<String, dynamic>(key, valueNew);
    }
    return MapEntry<String, dynamic>(key, value);
  });

  List results = await (FlutterMongoRealm.updateDocument(
    collectionName: this.collectionName,
    databaseName: this.databaseName,
    filter: BsonDocument(filter).toJson(),
    update: BsonDocument(updateValues).toJson(),
  ));

  return results;
}