File: indexes.md

package info (click to toggle)
python-beanie 1.29.0-1
  • links: PTS, VCS
  • area: main
  • in suites: sid, trixie
  • size: 1,544 kB
  • sloc: python: 14,413; makefile: 7; sh: 6
file content (87 lines) | stat: -rw-r--r-- 2,482 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
## Indexes setup

There are more than one way to set up indexes using Beanie

### Indexed function

To set up an index over a single field, the `Indexed` function can be used to wrap the type 
and does not require a `Settings` class:

```python
from beanie import Document, Indexed


class Sample(Document):
    num: Annotated[int, Indexed()]
    description: str
```

The `Indexed` function takes an optional `index_type` argument, which may be set to a pymongo index type:

```python
import pymongo

from beanie import Document, Indexed


class Sample(Document):
    description: Annotated[str, Indexed(index_type=pymongo.TEXT)]
```

The `Indexed` function also supports PyMongo's `IndexModel` kwargs arguments (see the [PyMongo Documentation](https://pymongo.readthedocs.io/en/stable/api/pymongo/operations.html#pymongo.operations.IndexModel) for details). 
 
For example, to create a `unique` index:

```python
from beanie import Document, Indexed


class Sample(Document):
    name: Annotated[str, Indexed(unique=True)]
```

The `Indexed` function can also be used directly in the type annotation, by giving it the wrapped type as the first argument. Note that this might not work with some Pydantic V2 types, such as `UUID4` or `EmailStr`.

```python
from beanie import Document, Indexed


class Sample(Document):
    name: Indexed(str, unique=True)
```

### Multi-field indexes

The `indexes` field of the inner `Settings` class is responsible for more complex indexes. 
It is a list where items can be:

- Single key. Name of the document's field (this is equivalent to using the Indexed function described above without any additional arguments)
- List of (key, direction) pairs. Key - string, name of the document's field. Direction - pymongo direction (
  example: `pymongo.ASCENDING`)
- `pymongo.IndexModel` instance - the most flexible
  option. [PyMongo Documentation](https://pymongo.readthedocs.io/en/stable/api/pymongo/operations.html#pymongo.operations.IndexModel)

```python
import pymongo
from pymongo import IndexModel

from beanie import Document


class Sample(Document):
    test_int: int
    test_str: str

    class Settings:
        indexes = [
            "test_int",
            [
                ("test_int", pymongo.ASCENDING),
                ("test_str", pymongo.DESCENDING),
            ],
            IndexModel(
                [("test_str", pymongo.DESCENDING)],
                name="test_string_index_DESCENDING",
            ),
        ]
```