---
title: "Perform a Lazy Query"
url: "https://docs.ravendb.net/6.2/client-api/session/querying/how-to-perform-queries-lazily"
version: "6.2"
language: "nodejs"
---

# Perform a Lazy Query

> Client language: Node.js. Also available for [C#](https://docs.ravendb.net/6.2/client-api/session/querying/how-to-perform-queries-lazily.md), [Java](https://docs.ravendb.net/6.2/client-api/session/querying/how-to-perform-queries-lazily.java.md), [Python](https://docs.ravendb.net/6.2/client-api/session/querying/how-to-perform-queries-lazily.python.md), [PHP](https://docs.ravendb.net/6.2/client-api/session/querying/how-to-perform-queries-lazily.php.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."

> **Note**
>
> - Query execution can be deferred: the query can be defined as **Lazy**, and executed
>   at a later time, when its results are actually needed.
>
> - This article contains examples for lazy queries. Prior to reading it, please refer
>   to [perform requests lazily](https://docs.ravendb.net/6.2/client-api/session/how-to/perform-operations-lazily.nodejs.md)
>   for general knowledge about RavenDB's lazy behavior and other request types that can be
>   executed lazily within a session.
>
> - In this page:
>   - [Lazy query](#lazy-query)
>   - [Lazy count query](#lazy-count-query)
>   - [Lazy suggestions query](#lazy-suggestions-query)
>   - [Lazy facets query](#lazy-facets-query)
>   - [Syntax](#syntax)

## Lazy query

```javascript
// Define a lazy query
const lazyEmployees = session
  .query({ collection: "Employees" })
  .whereEquals("FirstName", "Robert")
   // Add a call to 'lazily'
  .lazily();

const employees = await lazyEmployees.getValue(); // Query is executed here
```

- Learn more about queries in this [query overview](https://docs.ravendb.net/6.2/client-api/session/querying/how-to-query.nodejs.md).

## Lazy count query

```javascript
// Define a lazy count query
const lazyCount = session
  .query({ collection: "Employees" })
  .whereEquals("FirstName", "Robert")
   // Add a call to 'countLazily'
  .countLazily();

const count = await lazyCount.getValue(); // Query is executed here
```

## Lazy suggestions query

```javascript
// Define a lazy suggestion query
const lazySuggestions = session
  .query({ collection: "Products" })
  .suggestUsing(builder => builder.byField("Name", "chaig"))
   // Add a call to 'executeLazy'
  .executeLazy();

const suggestResult = await lazySuggestions.getValue(); // Query is executed here
const suggestions = suggestResult["Name"].suggestions;
```

- Learn more about suggestions in [query for suggestions](https://docs.ravendb.net/6.2/client-api/session/querying/how-to-work-with-suggestions.nodejs.md).

## Lazy facets query

### Lazy\_query

```javascript
// The facets definition used in the facets query:
// ===============================================
const categoryNameFacet = new Facet();

categoryNameFacet.fieldName = "categoryName";
categoryNameFacet.displayFieldName = "Product Category";

const rangeFacet = new RangeFacet();
rangeFacet.ranges = [
  "pricePerUnit < " + 25,
  "pricePerUnit >= " + 25 + " and pricePerUnit < " + 50,
  "pricePerUnit >= " + 50 + " and pricePerUnit < " + 100,
  "pricePerUnit >= " + 100
];
rangeFacet.displayFieldName = 'Price per Unit';

const facetsDefinition = [categoryNameFacet, rangeFacet];

// The lazy factes query:
// ======================
const lazyFacets = session
  .query({ indexName: "Products/ByCategoryAndPrice" })
  .aggregateBy(...facetsDefinition)
   // Add a call to 'executeLazy'
  .executeLazy();

const facets = await lazyFacets.getValue(); // Query is executed here

const categoryResults = facets["Product Category"];
const priceResults = facets["Price per Unit"];
```

### Index\_definition

```javascript
// The index definition used in the facets query:
class Products_ByCategoryAndPrice extends AbstractJavaScriptIndexCreationTask {
  constructor() {
      super();

      const { load } = this.mapUtils();

      this.map("Products", product => {
          return {
              categoryName: load(product.Category, "Categories").Name,
              pricePerUnit: product.PricePerUnit
          }
      });
  }
}
```

- Learn more about facets in [perform faceted search](https://docs.ravendb.net/6.2/client-api/session/querying/how-to-perform-a-faceted-search.nodejs.md).

## Syntax

```javascript
lazily();
lazily(onEval);

countLazily();

executeLazy();
```

| Parameters | Type | Description |
| - | - | - |
| **onEval** | `(object[]) => void` | An action that will be performed on the query results<br />when the query is executed. |

| Return Value | |
| - | - |
| **object** | A `Lazy` instance that will evaluate the query only when needed. |

## See also

- [Query overview](https://docs.ravendb.net/6.2/client-api/session/querying/how-to-query.nodejs.md)
- [Perform requests lazily](https://docs.ravendb.net/6.2/client-api/session/how-to/perform-operations-lazily.nodejs.md)
- [Querying: Basics](https://docs.ravendb.net/6.2/indexes/querying/query-index.nodejs.md)
- [query for suggestions](https://docs.ravendb.net/6.2/client-api/session/querying/how-to-work-with-suggestions.nodejs.md)
