Interface MongoCursor<TResult>

Type Parameters:
TResult - The type of documents the cursor contains
All Superinterfaces:
AutoCloseable, Closeable, Iterator<TResult>
All Known Subinterfaces:
MongoChangeStreamCursor<TResult>

@NotThreadSafe public interface MongoCursor<TResult> extends Iterator<TResult>, Closeable
The Mongo Cursor interface implementing the iterator protocol.

An application should ensure that a cursor is closed in all circumstances, e.g. using a try-with-resources statement:


 try (MongoCursor<Document> cursor = collection.find().cursor()) {
     while (cursor.hasNext()) {
         System.out.println(cursor.next());
     }
 }
 
Since:
3.0
  • Method Summary

    Modifier and Type
    Method
    Description
    int
    Gets the number of results available locally without blocking, which may be 0.
    void
    Despite this interface being non-thread-safe, close() is allowed to be called concurrently with any method of the cursor, including itself.
    default void
     
    Returns the server address
    Returns the server cursor, which can be null if the no cursor was created or if the cursor has been exhausted or killed.
    boolean
     
     
    A special next() case that returns the next element in the iteration if available or null.

    Methods inherited from interface java.util.Iterator

    remove
  • Method Details

    • close

      void close()
      Despite this interface being non-thread-safe, close() is allowed to be called concurrently with any method of the cursor, including itself. This is useful to cancel blocked hasNext(), next(). This method is idempotent.
      Specified by:
      close in interface AutoCloseable
      Specified by:
      close in interface Closeable
    • hasNext

      boolean hasNext()
      Specified by:
      hasNext in interface Iterator<TResult>
    • next

      TResult next()
      Specified by:
      next in interface Iterator<TResult>
    • available

      int available()
      Gets the number of results available locally without blocking, which may be 0.

      If the cursor is known to be exhausted, returns 0. If the cursor is closed before it's been exhausted, it may return a non-zero value.

      Returns:
      the number of results available locally without blocking
      Since:
      4.4
    • tryNext

      @Nullable TResult tryNext()
      A special next() case that returns the next element in the iteration if available or null.

      Tailable cursors are an example where this is useful. A call to tryNext() may return null, but in the future calling tryNext() would return a new element if a document had been added to the capped collection.

      Returns:
      the next element in the iteration if available or null.
      MongoDB documentation
      Tailable Cursor
    • getServerCursor

      @Nullable ServerCursor getServerCursor()
      Returns the server cursor, which can be null if the no cursor was created or if the cursor has been exhausted or killed.
      Returns:
      the ServerCursor, which can be null.
    • getServerAddress

      ServerAddress getServerAddress()
      Returns the server address
      Returns:
      ServerAddress
    • forEachRemaining

      default void forEachRemaining(Consumer<? super TResult> action)
      Specified by:
      forEachRemaining in interface Iterator<TResult>