modernFindOne method

Future<Map<String, dynamic>?> modernFindOne({
  1. SelectorBuilder? selector,
  2. Map<String, dynamic>? filter,
  3. Map<String, Object>? sort,
  4. Map<String, Object>? projection,
  5. String? hint,
  6. Map<String, Object>? hintDocument,
  7. int? skip,
  8. FindOptions? findOptions,
  9. Map<String, Object>? rawOptions,
})

Returns one document that satisfies the specified query criteria on the collection or view. If multiple documents satisfy the query, this method returns the first document according to the sort order or the natural order of sort parameter is not specified. In capped collections, natural order is the same as insertion order. If no document satisfies the query, the method returns null.

In MongoDb this method only allows the filter and the projection parameters. This version has more parameters, and it is essentially a wrapper araound the find method with a fixed limit set to 1 that returns a document instead of a stream.

Implementation

Future<Map<String, dynamic>?> modernFindOne(
    {SelectorBuilder? selector,
    Map<String, dynamic>? filter,
    Map<String, Object>? sort,
    Map<String, Object>? projection,
    String? hint,
    Map<String, Object>? hintDocument,
    int? skip,
    FindOptions? findOptions,
    Map<String, Object>? rawOptions}) async {
  if (!db.masterConnection.serverCapabilities.supportsOpMsg) {
    throw MongoDartError('At least MongoDb version 3.6 is required '
        'to run the findOne operation');
  }

  var sortMap = sort;
  if (sortMap == null && selector?.map[keyOrderby] != null) {
    sortMap = <String, Object>{...selector!.map[keyOrderby]};
  }
  var operation = FindOperation(this,
      filter:
          filter ?? (selector?.map == null ? null : selector!.map[key$Query]),
      sort: sortMap,
      projection: projection ?? selector?.paramFields,
      hint: hint,
      hintDocument: hintDocument,
      limit: 1,
      skip: skip ??
          (selector != null && selector.paramSkip > 0
              ? selector.paramSkip
              : null),
      findOptions: findOptions,
      rawOptions: rawOptions);

  return ModernCursor(operation).nextObject();
}