---
title: "Stream Query Results"
url: "https://docs.ravendb.net/6.2/client-api/session/querying/how-to-stream-query-results"
version: "6.2"
language: "java"
---

# Stream Query Results

> Client language: Java. Also available for [C#](https://docs.ravendb.net/6.2/client-api/session/querying/how-to-stream-query-results.md), [Node.js](https://docs.ravendb.net/6.2/client-api/session/querying/how-to-stream-query-results.nodejs.md).
>
> RavenDB publishes an official agent skill for coding assistants at https://docs.ravendb.net/skills/ravendb/SKILL.md (also installable with `npx skills add ravendb/skills`). The skill describes itself as: "Read this before writing any RavenDB query, client call or /databases/ request. Covers server setup, document modelling, the Node.js, .NET and Python clients, RQL, indexes, vector search and AI agents. Code written from recall tends to run and return the wrong rows, because RQL resembles SQL and the clients take field names as plain strings."

RavenDB supports **streaming data** from the server to the client.  
Streaming is useful when processing a large number of results.

The data streamed can be a result of a dynamic query, a static index query, or just filtered by a prefix.

To stream results, use the `stream` method from the `advanced` session operations.

## Streaming overview

- **Immediate processing**:  
  Neither the client nor the server holds the full response in memory.  
  Instead, as soon as the server has a single result, it sends it to the client.  
  Thus, your application can start processing results before the server sends them all.

- **No tracking**:  
  The stream results are Not tracked by the session.  
  Changes made to the resulting entities will not be sent to the server when *saveChanges* is called.

- **A snapshot of the data**:  
  The stream results are a snapshot of the data at the time when the query is computed by the server.  
  Results that match the query after it was already processed are Not streamed to the client.

- **Query limitations:**:

  - A streaming query does not wait for indexing by design.  
    So calling [waitForNonStaleResults](https://docs.ravendb.net/6.2/client-api/session/querying/how-to-customize-query.java.md#waitfornonstaleresults) is Not supported and will result in an exception.

  - Using [include](https://docs.ravendb.net/6.2/client-api/session/loading-entities.java.md#load-with-includes) to load a related document to the session in a streaming query is Not supported.

## Syntax

```java
<T> CloseableIterator<StreamResult<T>> stream(IDocumentQuery<T> query);

<T> CloseableIterator<StreamResult<T>> stream(IDocumentQuery<T> query, Reference<StreamQueryStatistics> streamQueryStats);

<T> CloseableIterator<StreamResult<T>> stream(IRawDocumentQuery<T> query);

<T> CloseableIterator<StreamResult<T>> stream(IRawDocumentQuery<T> query, Reference<StreamQueryStatistics> streamQueryStats);
```

| Parameters | | |
| - | - | - |
| **query** | [IDocumentQuery](https://docs.ravendb.net/6.2/client-api/session/querying/how-to-query.md#sessionadvanceddocumentquery) or [IRawDocumentQuery](https://docs.ravendb.net/6.2/client-api/session/querying/how-to-query.md#sessionadvancedrawquery) | Query to stream results for. |
| `Reference` **streamQueryStats** | StreamQueryStatistics | Information about performed query. |

| Return Value | |
| - | - |
| CloseableIterator\<StreamResult> | Iterator with entities. |

## Example I - Using Static Index

```java
IDocumentQuery<Employee> query = session
  .query(Employee.class, Employees_ByFirstName.class)
  .whereEquals("FirstName", "Robert");

CloseableIterator<StreamResult<Employee>> results = session.advanced().stream(query);

while (results.hasNext()) {
  StreamResult<Employee> employee = results.next();
}
```

## Example II - Dynamic Document Query

```java
IDocumentQuery<Employee> query = session
  .advanced()
  .documentQuery(Employee.class)
  .whereEquals("FirstName", "Robert");

Reference<StreamQueryStatistics> streamQueryStatsRef = new Reference<>();
CloseableIterator<StreamResult<Employee>> results = session.advanced().stream(query, streamQueryStatsRef);

while (results.hasNext()) {
  StreamResult<Employee> employee = results.next();
}
```

## Example III - Dynamic Raw Query

```java
IRawDocumentQuery<Employee> query = session.advanced()
  .rawQuery(Employee.class, "from Employees where FirstName = 'Robert'");

CloseableIterator<StreamResult<Employee>> results = session.advanced().stream(query);

while (results.hasNext()) {
  StreamResult<Employee> employee = results.next();
}
```

## See also

- [How to Query](https://docs.ravendb.net/6.2/client-api/session/querying/how-to-query.md)
- [What is a Document Query](https://docs.ravendb.net/6.2/client-api/session/querying/document-query/what-is-document-query.java.md)
- [Streaming data](https://ravendb.net/learn/inside-ravendb-book/reader/4.0/4-deep-dive-into-the-ravendb-client-api#streaming-data)
