Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add user documentation about unique index creation #2856

Merged
merged 5 commits into from
Jun 19, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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

AlekSi marked this conversation as resolved.
Show resolved Hide resolved
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
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@rumyantseva A separate section may be quite confusing since we have one on how to create indexes. Let's add this to the section above on how to create indexes.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It can be a subsection there too or just as extra details after this sentence You can create single field indexes or compound indexes.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

"Index creation details" is a subsection of "How to create indexes", not a top-level section

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@Fashander Please send a new PR to improve that page if needed


- 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