find method

Future<List<MongoDocument>> find({
  1. dynamic filter,
  2. RemoteFindOptions? options,
})

Finds all documents in the collection according to the given filter

Implementation

Future<List<MongoDocument>> find({filter, RemoteFindOptions? options}) async {
  var filterCopy = <String, dynamic>{};
  if (filter != null) {
    assert(filter is LinkedHashMap<String, dynamic> ||
        filter is LogicalQueryOperator);

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

  var sortMap = options?.sort?.map((k, v) => MapEntry(k, v.value));
  var projectionMap =
      options?.projection?.map((k, v) => MapEntry(k, v.value));

  List<dynamic>? resultJson = await (FlutterMongoRealm.findDocuments(
    collectionName: this.collectionName,
    databaseName: this.databaseName,
    filter: BsonDocument(filterCopy).toJson(),
    projection: projectionMap == null ? null : jsonEncode(projectionMap),
    limit: options?.limit ?? 0,
    sort: sortMap == null ? null : jsonEncode(sortMap),
  ));

  if (resultJson == null) {
    return [];
  }

  if (resultJson == null) {
    return [];
  }

  var result = resultJson.map((string) {
    return MongoDocument.parse(string);
  }).toList();

  return result;
}