---
title: "Querying Time Series Indexes"
url: "https://docs.ravendb.net/6.2/document-extensions/timeseries/querying/using-indexes"
version: "6.2"
language: "nodejs"
---

# Querying Time Series Indexes

> Client language: Node.js. Also available for [C#](https://docs.ravendb.net/6.2/document-extensions/timeseries/querying/using-indexes.md), [Python](https://docs.ravendb.net/6.2/document-extensions/timeseries/querying/using-indexes.python.md), [PHP](https://docs.ravendb.net/6.2/document-extensions/timeseries/querying/using-indexes.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**
>
> - **Time series index**:
>
>   - STATIC-time-series-indexes can be defined from the [Client API](https://docs.ravendb.net/6.2/document-extensions/timeseries/indexing.nodejs.md)
>     or using [Studio](https://docs.ravendb.net/6.2/studio/database/indexes/create-map-index.md).  
>     Such an index can be queried in the same way as a regular index that indexes documents.  
>     (See [Querying an index](https://docs.ravendb.net/6.2/indexes/querying/query-index.nodejs.md)).
>
>   - AUTO-time-series-indexes are Not generated automatically by the server when making a time series query.
>
> - **The contents of the query results**:
>
>   - Unlike a document index, where the source data are your JSON documents,  
>     the source data for a time series index are the time series entries within the documents.
>
>   - When querying a **document index**:  
>     the resulting objects are the document entities (unless results are [projected](https://docs.ravendb.net/6.2/indexes/querying/projections.nodejs.md)).
>
>   - When querying a **time series index**:  
>     each item in the results is of the type defined by the **index-entry** in the index definition,  
>     (unless results are [projected](#project-results)).
>     The documents themselves are not returned.
>
> - In this page:
>   - [Sample index](#sample-index)
>   - [Querying the index](#querying-the-index)
>     - [Query all time series entries](#query-all-time-series-entries)
>     - [Filter query results](#filter-query-results)
>     - [Order query results](#order-query-results)
>     - [Project results](#project-results)

## Sample Index

- The following is a time series map-index that will be used in the query examples throughout this article.

- Each **index-entry** consists of:
  - Three index-fields obtained from the "HeartRates" time series entries: `bpm`, `date`, and `tag`.
  - One index-field obtained from the time series [segment](https://docs.ravendb.net/6.2/document-extensions/timeseries/indexing.nodejs.md#timeseriessegment-object) header: `employeeID`.
  - One index-field obtained from the loaded employee document: `employeeName`.

- When querying this time series index:
  - The resulting items correspond to the time series entries that match the query predicate.
  - Each item in the results will in the shape of the defined index-entry.  
    Different result types may be returned when the query [projects the results](#project-results).

```javascript
class TsIndex extends AbstractRawJavaScriptTimeSeriesIndexCreationTask {
  constructor() {
      super();

      this.maps.add(`
          timeSeries.map("Employees", "HeartRates", function (segment) {
               let employee = load(segment.DocumentId, "Employees")
               
               // Return the index-entry:
               return segment.Entries.map(entry => ({
               
                   // Define the index-fields:
                   bpm: entry.Values[0],
                   date: new Date(entry.Timestamp),
                   tag: entry.Tag
                   employeeID: segment.DocumentId,
                   employeeName: employee.FirstName + " " + employee.LastName
               }));
          })
      `;
  }
}
```

## Querying the index

#### Query all time series entries:

No filtering is applied in this query.  
Results will include ALL entries from time series "HeartRates".

##### Query

```javascript
const results = await session
   // Query the index 
  .query({ indexName: "TsIndex" })
   // Query for all entries w/o any filtering
  .all();

// Access results:
const entryResult = results[0];
const employeeName = entryResult.employeeName;
const bpm = entryResult.bpm;
```

##### RawQuery

```javascript
const results = await session
   // Provide RQL to rawQuery
  .advanced.rawQuery("from index 'TsIndex'")
   // Execute the query
  .all();
```

##### RQL

```sql
from index "TsIndex"
```

#### Filter query results:

In this example, time series entries are filtered by the query.  
The query predicate is applied to the index-fields.

##### Query

```javascript
const results = await session
  .query({ indexName: "TsIndex" })
   // Retrieve only time series entries with high BPM values for a specific employee
  .whereEquals("employeeName", "Robert King")
  .whereGreaterThanOrEqual("bpm", 85)
  .all();
```

##### RawQuery

```javascript
const results = await session
   // Retrieve only time series entries with high BPM values for a specific employee
  .advanced.rawQuery(`
       from index "TsIndex"
       where employeeName == "Robert King" and bpm > 85.0
  `)
  .all();
```

##### RQL

```sql
from index "TsIndex"
where employeeName == "Robert King" and bpm >= 85
```

#### Order query results:

Results can be ordered by any of the index-fields.

##### Query

```javascript
const results = await session
  .query({ indexName: "TsIndex" })
   // Retrieve time series entries where employees had a low BPM value
  .whereLessThan("bpm", 58)
   // Order by the 'date' index-field (descending order)
  .orderByDescending("date")
  .all();
```

##### RawQuery

```javascript
const results = await session
   // Retrieve entries with low BPM value and order by 'date' descending
  .advanced.rawQuery(`
        from index "TsIndex"
        where bpm < 58
        order by date desc
  `)
  .all();
```

##### RQL

```sql
from index "TsIndex"
where bpm < 58
order by date desc
```

#### Project results:

- Instead of returning the entire index entry object for each result item,  
  you can return only partial fields.

- Learn more about projecting query results in [Project Index Query Results](https://docs.ravendb.net/6.2/indexes/querying/projections.nodejs.md).

- In this example, we query for time series entries with a very high BPM value.  
  We retrieve entries with BPM value > 100 but return only the *employeeID* for each entry.

##### Query

```javascript
const results = await session
  .query({ indexName: "TsIndex" })
  .whereGreaterThanOrEqual("bpm", 100)
   // Return only the employeeID index-field in the results
  .selectFields(["employeeID"])
   // Optionally: call 'distinct' to remove duplicates from results
  .distinct()
  .all();
```

##### RawQuery

```javascript
const results = await session
   // Return only the employeeID index-field in the results
  .advanced.rawQuery(`
       from index "TsIndex"
       where bpm >= 100
       select distinct employeeID
  `)
  .all();
```

##### RQL

```sql
from index "TsIndex"
where bpm > 100
select distinct employeeID
```

## See also

- [Time Series Overview](https://docs.ravendb.net/6.2/document-extensions/timeseries/overview.md)
- [Studio Time Series Management](https://docs.ravendb.net/6.2/studio/database/document-extensions/time-series.md)
- [Time Series Indexing](https://docs.ravendb.net/6.2/document-extensions/timeseries/indexing.nodejs.md)
- [Range Selection](https://docs.ravendb.net/6.2/document-extensions/timeseries/querying/choosing-query-range.nodejs.md)
- [Filtering](https://docs.ravendb.net/6.2/document-extensions/timeseries/querying/filtering.nodejs.md)
- [Aggregation and Projection](https://docs.ravendb.net/6.2/document-extensions/timeseries/querying/aggregation-and-projections.nodejs.md)
- [Time Series Rollup and Retention](https://docs.ravendb.net/6.2/document-extensions/timeseries/rollup-and-retention.nodejs.md)
