You are currently browsing legacy 4.0 version of documentation. Click here to switch to the newest 4.2 version.

We can help you with migration to the latest RavenDB

Contact Us Now
see on GitHub

Session: Querying: How to Stream Query Results

Query results can be streamed using the stream() method from the advanced session operations. The query can be issued using either a static index, or it can be a dynamic one where it will be handled by an auto index.

Syntax

session.advanced.stream(query, [statsCallback], [callback]);

Parameters
query IDocumentQuery or IRawDocumentQuery Query to stream results for.
statsCallback function callback returning information about the streaming query (amount of results, which index was queried, etc.)
callback function returns a readable stream with query results (same as Return Value result below)
Return Value
Promise<Readable> A Promise resolving to readable stream with query results

Example I - Using Static Index

const query = session
    .query({ indexName: "Employees/ByFirstName" })
    .whereEquals("FirstName", "Robert");

// stream() returns Node.js Readable 
const stream = await session.advanced.stream(query);

stream.on("data", data => {
    // Employee { FirstName: "Robert", LastName: "Smith" id: "employees/1-A" }
});

stream.on("error", err => {
    // handle errors
});

stream.on("end", () => {
    // stream ended
});

Example II - Dynamic Document Query

const query = session.advanced
    .documentQuery(Employee)
    .whereEquals("FirstName", "Robert");

let streamStats;
const stream = await session.advanced
    .stream(query, stats => streamStats = stats);

stream.on("data", data => {
    // Employee { FirstName: "Robert", LastName: "Smith" id: "employees/1-A" }
});

stream.on("error", err => {
    // handle errors
});

stream.on("end", () => {
    // stream ended
});

Example III - Dynamic Raw Query

const rawQuery = session.advanced
    .rawQuery("from Employees where FirstName = 'Robert'");

const stream = session.advanced.stream(rawQuery);

stream.on("data", data => {
    // Employee { FirstName: "Robert", LastName: "Smith" id: "employees/1-A" }
});

stream.on("error", err => {
    // handle errors
});

stream.on("end", () => {
    // stream ended
});