Skip to content

Commit

Permalink
Add user documentation about unique index creation (FerretDB#2856)
Browse files Browse the repository at this point in the history
  • Loading branch information
Elena Grahovac authored Jun 19, 2023
1 parent d38ab72 commit 2e5997c
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 5 deletions.
26 changes: 22 additions & 4 deletions website/docs/indexes.md
Original file line number Diff line number Diff line change
Expand Up @@ -45,22 +45,40 @@ If it's `-1`, it specifies a descending order direction for the index.
### Compound Indexes

For compound indexes, you can create an index key combining multiple fields together as a key.
Below is an example of a compound index that uses `price` and `category` fields from the `products` collection as the index key.
Below is an example of a compound index that uses `price` and `category` fields
from the `products` collection as the index key:

```js
db.products.createIndex({ price: 1, category: 1 })
```

:::note
### Unique Indexes

You can create unique indexes to ensure that the indexed fields do not contain duplicate values.
To create a unique index, set the `unique` option as `true` when calling `createIndexes()` command.

Below is an example of a unique index for the `name` field from the `products` collection:

```js
db.products.createIndex({ name: 1 }, { unique: true })
```

Unique indexes can be compound.
Here is an example of a unique index consisting
of the `category` and `name` fields from the `products` collection:

```js
db.products.createIndex({ category: 1, name: 1 }, { unique: true })
```

### Index creation details

- If the `createIndexes()` command is called for a non-existent collection, it will create the collection and its given indexes.
- If the `createIndexes()` command is called for a non-existent field, an index for the field is created without creating or adding the field to an existing collection.
- If you attempt to create an index with the same name and key as an existing index, the system will not create a duplicate index.
Instead, it will simply return the name and key of the existing index, since duplicate indexes would be redundant and inefficient.
- Meanwhile, any attempt to call `createIndexes()` command for an existing index using the same name and different key, _or_ different name but the same key will return an error.

:::

## How to list Indexes

To display a collection's index details, use the `listIndexes()` command.
Expand Down
2 changes: 1 addition & 1 deletion website/docs/reference/supported-commands.md
Original file line number Diff line number Diff line change
Expand Up @@ -584,7 +584,7 @@ Related [issue](https://github.com/FerretDB/FerretDB/issues/1917).
| | `indexes` | || |
| | | `key` || |
| | | `name` | ✅️ | |
| | | `unique` | | [Unimplemented](https://github.com/FerretDB/FerretDB/issues/2045) |
| | | `unique` | | |
| | | `partialFilterExpression` || [Unimplemented](https://github.com/FerretDB/FerretDB/issues/2448) |
| | | `sparse` || [Unimplemented](https://github.com/FerretDB/FerretDB/issues/2448) |
| | | `expireAfterSeconds` || [Unimplemented](https://github.com/FerretDB/FerretDB/issues/2415) |
Expand Down

0 comments on commit 2e5997c

Please sign in to comment.