---
title: "Indexing Polymorphic Data"
url: "https://docs.ravendb.net/6.2/indexes/indexing-polymorphic-data"
version: "6.2"
language: "nodejs"
---

# Indexing Polymorphic Data

> Client language: Node.js. Also available for [C#](https://docs.ravendb.net/6.2/indexes/indexing-polymorphic-data.md), [Java](https://docs.ravendb.net/6.2/indexes/indexing-polymorphic-data.java.md), [Python](https://docs.ravendb.net/6.2/indexes/indexing-polymorphic-data.python.md), [PHP](https://docs.ravendb.net/6.2/indexes/indexing-polymorphic-data.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**
>
> - By default, RavenDB indexes are defined on a specific entity type, referred to as a `Collection`,  
>   and do not consider the inheritance hierarchy.
>
> - In this page:
>   - [The challenge](#the-challenge)
>   - [Possible solutions:](#possible-solutions)
>     - [Multi-map index](#multi-map-index)
>     - [Polymorphic index](#polymorphic-index)
>     - [Customize collection](#customize-collection)

## The challenge

Let's assume, for example, that we have the following inheritance hierarchy:

![Figure 1: Polymorphic indexes](https://docs.ravendb.net/assets/images/polymorphic_indexes_faq-58d4920423a9e5a92985e2844c906d35.png)

**By default**:  
When saving a `Cat` document, it will be assigned to the "Cats" collection,  
while a `Dog` document will be placed in the "Dogs" collection.

If we intend to create a simple Map-index for Cat documents based on their names, we would write:

### Map\_index

```javascript
class Cats_ByName extends AbstractJavaScriptIndexCreationTask {
  constructor() {
      super();

      // Index the 'name' field from the CATS collection
      this.map('Cats', cat => {
          return {
              name: cat.name
          };
      });
  }
}
```

### Class

```javascript
class Animal {
  constructor(name) {
      this.name = name;
  }
}

class Cat extends Animal { }
```

And for Dogs:

### Map\_index

```javascript
class Dogs_ByName extends AbstractJavaScriptIndexCreationTask {
  constructor() {
      super();

      // Index the 'name' field from the DOGS collection
      this.map('Dogs', dog => {
          return {
              name: dog.name
          };
      });
  }
}
```

### Class

```javascript
class Animal {
  constructor(name) {
      this.name = name;
  }
}

class Dog extends Animal { }
```

**The challenge**:  
Querying each index results in documents only from the specific collection the index was defined for.  
However, what if we need to query across ALL animal collections?

## Possible solutions

> **Note**
>
> &#x20;**Multi-Map Index**:
> Writing a [Multi-map index](https://docs.ravendb.net/6.2/indexes/multi-map-indexes.nodejs.md) enables getting results from all collections the index was defined for.
>
> ```javascript
> class CatsAndDogs_ByName extends AbstractJavaScriptMultiMapIndexCreationTask  {
>   constructor() {
>       super();
>
>       // Index documents from the CATS collection
>       this.map('Cats', cat => {
>           return {
>               name: cat.name
>           };
>       });
>
>       // Index documents from the DOGS collection
>       this.map('Dogs', dog => {
>           return {
>               name: dog.name
>           };
>       });
>   }
> }
> ```
>
> Query the Multi-map index:
>
> ### Query
>
> ```javascript
> const catsAndDogs = await session
>   // Query the index
>   .query({ indexName: "CatsAndDogs/ByName" })
>   // Look for all Cats or Dogs that are named 'Mitzy' :))
>   .whereEquals("name", "Mitzy")
>   .all();
>
> // Results will include matching documents from the CATS and DOGS collection
> ```
>
> ### RQL
>
> ```sql
> from index "CatsAndDogs/ByName"
> where name == "Mitzy"
> ```

> **Note**
>
> &#x20;**Polymorphic index**:
> Another option is to create a polymorphic-index.
>
> Use method `WhereEntityIs` within your index definition to index documents from all collections  
> listed in the method.
>
> ```javascript
> class CatsAndDogs_ByName extends AbstractCsharpIndexCreationTask {
>   constructor() {
>       super();
>
>       // Index documents from both the CATS collection and the DOGS collection
>       this.map = `from animal in docs.WhereEntityIs("Cats", "Dogs")
>                   select new {
>                       animal.name
>                   }`;
>   }
> }
> ```
>
> Query the polymorphic-index:
>
> ### Query
>
> ```javascript
> const catsAndDogs = await session
>   // Query the index
>   .query({ indexName: "CatsAndDogs/ByName" })
>   // Look for all Cats or Dogs that are named 'Mitzy' :))
>   .whereEquals("name", "Mitzy")
>   .all();
>
> // Results will include matching documents from the CATS and DOGS collection
> ```
>
> ### RQL
>
> ```sql
> from index "CatsAndDogs/ByName"
> where name == "Mitzy"
> ```

> **Note**
>
> &#x20;**Customize collection**:
> This option involves customizing the collection name that is assigned to documents created from  
> subclasses of the *Animal* class.
>
> This is done by setting the [findCollectionName](https://docs.ravendb.net/6.2/client-api/configuration/conventions.nodejs.md#findcollectionname) convention on the document store.
>
> ```javascript
> const documentStore = new DocumentStore(["serverUrl_1", "serverUrl_2", "..."], "DefaultDB");
>
> // Customize the findCollectionName convention 
> documentStore.conventions.findCollectionName = (type) => {
>   const typeName = type.name;
>
>   // Documents created from a 'Cat' or a 'Dog' entity will be assinged the "Animals" collection
>   if (typeName === "Cat" || typeName === "Dog") {
>       return "Animals";
>   }
>
>   // All other documents will be assgined the default collection name
>   return DocumentConventions.defaultGetCollectionName(type);
> }
> ```
>
> With the above convention in place, whenever a *Cat* or a *Dog* entity is saved, its document will be assigned the "Animals" collection instead of the default "Cats" or "Dogs" collection.
>
> Now you can define a Map-index on the "Animals" collection:
>
> ```javascript
> class Animals_ByName extends AbstractJavaScriptIndexCreationTask {
>   constructor() {
>       super();
>
>       // Index documents from the ANIMALS collection
>       this.map('Animals', animal => {
>           return {
>               name: animal.name
>           };
>       });
>   }
> }
> ```
>
> Query the index:
>
> ### Query
>
> ```javascript
> const animals = await session
>   // Query the index
>   .query({ indexName: "Animals/ByName" })
>   // Look for all Animals that are named 'Mitzy' :))
>   .whereEquals("name", "Mitzy")
>   .all();
>
> // Results will include matching documents from the ANIMALS collection
> ```
>
> ### RQL
>
> ```sql
> from index "Animals/ByName"
> where name == "Mitzy"
> ```

## See also

- [Indexing basics](https://docs.ravendb.net/6.2/indexes/indexing-basics.nodejs.md)
- [Indexing related documents](https://docs.ravendb.net/6.2/indexes/indexing-related-documents.nodejs.md)
- [Indexing spatial data](https://docs.ravendb.net/6.2/indexes/indexing-spatial-data.nodejs.md)
- [Indexing hierarchical data](https://docs.ravendb.net/6.2/indexes/indexing-hierarchical-data.nodejs.md)
- [Query overview](https://docs.ravendb.net/6.2/client-api/session/querying/how-to-query.nodejs.md)
- [Basics](https://docs.ravendb.net/6.2/indexes/querying/query-index.nodejs.md)
