modules/core/nuxeo-core-storage-mongodb/src/main/java/org/nuxeo/ecm/core/storage/mongodb/MongoDBConnection.java
/**
     * Logs, runs request and constructs a closeable {@link Stream} on top of {@link MongoCursor}.
     * <p />
     * We should rely on this method, because it correctly handles cursor closed state.
     * <p />
     * Note: Looping on {@link FindIterable} or {@link MongoIterable} could lead to cursor leaks. This is also the case
     * on some call to {@link MongoIterable#first()}.
     *
     * @return a closeable {@link Stream} instance linked to {@link MongoCursor}
     */
Logs, runs request and constructs a closeable  Stream on top of  MongoCursor.

We should rely on this method, because it correctly handles cursor closed state.

Note: Looping on  FindIterable or  MongoIterable could lead to cursor leaks. This is also the case
on some call to  MongoIterable#first().

@return a closeable  Stream instance linked to  MongoCursor
Logs runs request and constructs a closeable Stream on top of MongoCursor . We should rely on this method because it correctly handles cursor closed state . Note : Looping on FindIterable or MongoIterable could lead to cursor leaks . This is also the case on some call to MongoIterable#first () . @return a closeable Stream instance linked to MongoCursor
protected Stream<State> stream(Bson filter, Bson projection, int limit) {
        if (filter == null) {
            filter = new Document();
        }
        logQuery(filter, projection);
        boolean completedAbruptly = true;
        MongoCursor<Document> cursor = find(filter).limit(limit).projection(projection).iterator();
        try {
            Set<Object> seen = new HashSet<>();
            Stream<State> stream = StreamSupport.stream(Spliterators.spliteratorUnknownSize(cursor, 0), false) 
                                                .onClose(cursor::close)
                                                .filter(doc -> seen.add(doc.get(idKey)))
                                                 MongoDB cursors may return the same
                                                 object several times
                                                .map(converter::bsonToState);
             the stream takes responsibility for closing the session
            completedAbruptly = false;
            return stream;
        } catch (MongoExecutionTimeoutException e) {
            throw newQueryTimeout(e, filter);  NOSONAR (cursor is not leaked)
        } finally {
            if (completedAbruptly) {
                cursor.close();
            }
        }
    }
protected Stream < State > stream ( Bson filter , Bson projection , int limit ) { if ( filter == null ) { filter = new Document ( ) ; } logQuery ( filter , projection ) ; boolean completedAbruptly = true ; MongoCursor < Document > cursor = find ( filter ) . limit ( limit ) . projection ( projection ) . iterator ( ) ; try { Set < Object > seen = new HashSet < > ( ) ; Stream < State > stream = StreamSupport . stream ( Spliterators . spliteratorUnknownSize ( cursor , 0 ) , false ) . onClose ( cursor :: close ) . filter ( doc -> seen . add ( doc . get ( idKey ) ) ) . map ( converter :: bsonToState ) ; completedAbruptly = false ; return stream ; } catch ( MongoExecutionTimeoutException e ) { throw newQueryTimeout ( e , filter ) ; } finally { if ( completedAbruptly ) { cursor . close ( ) ; } } }
Logs, runs request and constructs a closeable {@link Stream} on top of {@link MongoCursor}.
Logs runs request and constructs a closeable { @link Stream } on top of { @link MongoCursor } .
// empty filter // it's ok if projection is null // // MongoDB cursors may return the same // object several times // the stream takes responsibility for closing the session // NOSONAR (cursor is not leaked)