---
title: "How to Get Tracked Entities"
url: "https://docs.ravendb.net/6.2/client-api/session/how-to/get-tracked-entities"
version: "6.2"
language: "nodejs"
---

# How to Get Tracked Entities

> Client language: Node.js. Also available for [C#](https://docs.ravendb.net/6.2/client-api/session/how-to/get-tracked-entities.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**
>
> - The Session [tracks all changes](https://docs.ravendb.net/6.2/client-api/session/what-is-a-session-and-how-does-it-work.nodejs.md#tracking-changes)
>   made to all the entities it has either loaded, stored, deleted, or queried for,  
>   and persists to the server only what is needed when `saveChanges()` is called.
>
> - You can use the session's advanced `getTrackedEntities()` method  
>   to retrieve the **list of all entities tracked by the session**.
>
> - To check what is the actual type of change made to the entities, see:  
>   [Get entity changes](https://docs.ravendb.net/6.2/client-api/session/how-to/check-if-entity-has-changed.nodejs.md#get-entity-changes), or
>   [Get session changes](https://docs.ravendb.net/6.2/client-api/session/how-to/check-if-there-are-any-changes-on-a-session.nodejs.md#get-session-changes).
>
> - In this page:
>   - [Get tracked entities](#get-tracked-entities)
>   - [Syntax](#syntax)

## Get tracked entities

**Tracking stored entities**:

```javascript
const session = documentStore.openSession();

// Store entities within the session:
let employee1 = new Employee();
employee.firstName = "John";
employee.lastName = "Doe";
await session.store(employee, "employees/1-A");

let employee2 = new Employee();
employee.firstName = "David";
employee.lastName = "Brown";
await session.store(employee2, "employees/2-A");

let employee3 = new Employee();
employee.firstName = "Tom";
employee.lastName = "Miller";
await session.store(employee3, "employees/3-A");

// Get tracked entities:
let trackedEntities = session.advanced.getTrackedEntities();

// The session tracks the new stored entities:
const entityInfo = trackedEntities["employees/1-A"];
assert.equal(entityInfo.id, "employees/1-A");
assert.ok(entityInfo.entity instanceof Employee);

// Save changes:
await session.saveChanges();

// The session keeps tracking the entities even after SaveChanges is called:
trackedEntities = session.advanced.getTrackedEntities();
```

**Tracking loaded and deleted entities**:

```javascript
const session = documentStore.openSession();

// Load entity:
const employee1 = await session.load("employees/1-A");

// Delete entity:
session.delete("employees/3-A");

// Get tracked entities:
const trackedEntities = session.advanced.getTrackedEntities();

// The session tracks the 2 entities:

// The loaded entity:
const entityInfo1 = trackedEntities["employees/1-A"];
assert.ok(!entityInfo1.isDeleted);

// The deleted entity:
const entityInfo2 = trackedEntities.get("employees/3-A");
assert.ok(entityInfo2.isDeleted);

// Save changes:
await session.saveChanges();
```

**Tracking queried entities**:

```javascript
const session = documentStore.openSession();

// Query for all employees:
const employees = await session.query({ collection: "employees"}).all();

// Get tracked entities:
const trackedEntities = session.advanced.getTrackedEntities();

// The session tracks the entities loaded via the query:
const entityInfo1 = trackedEntities[employees[0].id];
const entityInfo2 = trackedEntities[employees[1].id];
```

## Syntax

```javascript
getTrackedEntities();
```

| Return value | |
| - | - |
| `Map<string, EntityInfo>` | A dictionary that maps the entity id to the `EntityInfo` object. |

```javascript
class EntityInfo {
  // The tracked entity id
  id;       // string
  
  // The tracked entity object
  entity;   // object
  
  // isDeleted is true if entity was deleted
  isDeleted // boolean
}
```

## See also

- [What is a session and how does it work](https://docs.ravendb.net/6.2/client-api/session/what-is-a-session-and-how-does-it-work.nodejs.md)
- [How to check for entity changes](https://docs.ravendb.net/6.2/client-api/session/how-to/check-if-entity-has-changed.nodejs.md)
- [Evict entity from session](https://docs.ravendb.net/6.2/client-api/session/how-to/evict-entity-from-a-session.nodejs.md)
- [Refresh entity](https://docs.ravendb.net/6.2/client-api/session/how-to/refresh-entity.nodejs.md)
