<commit_before>---
layout: docs
title: 'Compound Index'
---

A compound (or composite) index is an index based on several keypaths. It can be used to efficiently index multiple properties in one index to easily find the existance of the combination of two keys and their values.

In Dexie, a compound index must be specified when defining the schema:

```javascript
var db = new Dexie('dbname');
db.version(1).stores({
    people: 'id, [firstName+lastName]'
});
```

In the above sample, records containing valid keys in the *firstName* and *lastName* properties will be indexed. If having stored an object with properties `{firstName: 'foo', lastName: 'bar'}`, it can be efficiently looked up using `db.people.where('[firstName+lastName]').equals(['foo', 'bar'])`.

```javascript
async function sample() {

    // Store person
    await db.people.put({
        id: 1,
        firstName: 'foo',
        lastName: 'bar'
    });
    
    // Query person:
    const fooBar = await db.people.get({
      firstName: 'foo',
      lastName: 'bar'
    });
    
    // Show result
    console.log ("Foobar: ", fooBar.id, fooBar.firstName, fooBar.lastName);<commit_msg>Update Compound-Index.md<commit_after>---
layout: docs
title: 'Compound Index'
---

A compound (or composite) index is an index based on several keypaths. It can be used to efficiently index multiple properties in one index to easily find the existance of the combination of two keys and their values.

In Dexie, a compound index must be specified when defining the schema:

```javascript
var db = new Dexie('dbname');
db.version(1).stores({
    people: 'id, [firstName+lastName]'
});
```

In the above sample, records containing valid keys in the *firstName* and *lastName* properties will be indexed. If having stored an object with properties `{firstName: 'foo', lastName: 'bar'}`, it can be efficiently looked up using `db.people.where('[firstName+lastName]').equals(['foo', 'bar'])` or in Dexie 2.0,  `db.people.where({firstName: 'foo', lastName: 'bar')`.

```javascript
async function sample() {

    // Store person
    await db.people.put({
        id: 1,
        firstName: 'foo',
        lastName: 'bar'
    });
    
    // Query person:
    const fooBar = await db.people.where({
      firstName: 'foo',
      lastName: 'bar'
    }).first();
    
    // Show result
    console.log ("Foobar: ", fooBar.id, fooBar.firstName, fooBar.lastName);
{'fraction_non_alphanumeric': 0.1281625881377022, 'fraction_numerical': 0.002488593944421402, 'mean_word_length': 3.4749536178107605, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '475896', 'n_tokens_mistral': 730, 'n_tokens_neox': 700, 'n_words': 275}