Add `first` and `firstOrNull` overloads to FindFlow<T>

XMLWordPrintableJSON

    • None
    • Java Drivers
    • None
    • None
    • None
    • None
    • None
    • None

      It'd be great to have an overload of the first and firstOrNull Flow operations on FindFlow that calls limit(1) automatically. Otherwise, an entire batch is unnecessarily requested.

      We have some code like this:

      collection
        .find<IdOnly>(filter)
        .projection(Projections.include("_id"))
        .limit(1)
        .firstOrNull()
      We have, on occasion, forgotten the .limit(1) part, which leads to us wasting time fetching results we'll never operate on. It'd be great if these were defined directly on FindFlow, so that we could get this behavior automagically.
       
      We have considered adding an extension function, but then we run the risk of importing the wrong extension function by mistake.

      I was thinking something like this:

      import kotlinx.coroutines.flow.first as baseFirst
      import kotlinx.coroutines.flow.firstOrNull as baseFirstOrNull

      suspend fun first(): T = limit(1).baseFirst()
      suspend fun firstOrNull(): T? = limit(1).baseFirstOrNull()

            Assignee:
            Ross Lawley
            Reporter:
            Mary Strodl (EXT)
            None
            Votes:
            0 Vote for this issue
            Watchers:
            3 Start watching this issue

              Created:
              Updated: