/// <summary> /// Gets a single result or null. /// </summary> /// <typeparam name="TDocument">The type of the document.</typeparam> /// <typeparam name="TProjection">The type of the projection (same as TDocument if there is no projection).</typeparam> /// <param name="find">The fluent find.</param> /// <param name="cancellationToken">The cancellation token.</param> /// <returns>A Task whose result is the single result or null.</returns> public static TProjection SingleOrDefault <TDocument, TProjection>(this IFindFluent <TDocument, TProjection> find, CancellationToken cancellationToken = default(CancellationToken)) { Ensure.IsNotNull(find, nameof(find)); if (!find.Options.Limit.HasValue || find.Options.Limit.Value > 2) { find = find.Limit(2); } return(IAsyncCursorSourceExtensions.SingleOrDefault(find, cancellationToken)); }
/// <summary> /// Returns the only document of the aggregate result, or the default value if the result set is empty. Throws an exception if the result set contains more than one document. /// </summary> /// <typeparam name="TResult">The type of the result.</typeparam> /// <param name="aggregate">The aggregate.</param> /// <param name="cancellationToken">The cancellation token.</param> /// <returns> /// The fluent aggregate interface. /// </returns> public static TResult SingleOrDefault <TResult>(this IAggregateFluent <TResult> aggregate, CancellationToken cancellationToken = default(CancellationToken)) { Ensure.IsNotNull(aggregate, nameof(aggregate)); return(IAsyncCursorSourceExtensions.SingleOrDefault(aggregate.Limit(2), cancellationToken)); }