It is possible to define nested linked documents with Beanie. Sometimes this can lead to infinite recursion. To prevent this, or to decrease the database load, you can limit the nesting depth. By default, it is set to 3, which means it will fetch up to 3 levels of nested documents.
You can configure:
maximum depth for all linked documents
depth for a specific linked document
Maximum:
class Sample(Document):
num: int
category: Link[Category]
class Settings:
max_nesting_depth = 2 # Maximum nesting depth for all linked documents of this model
Specific:
class Sample(Document):
num: int
category: Link[Category]
class Settings:
max_nesting_depths_per_field = {
"category": 1 # Nesting depth for a specific field
}
Find
You also can limit the nesting depth during find operations.
from beanie import Document, Link
from typing import Optional
class SelfLinkedSample(Document):
name: str
left: Optional[Link["SelfLinkedSample"]]
right: Optional[Link["SelfLinkedSample"]]
You can set up maximum depth for all linked documents:
Define a document
It is possible to define nested linked documents with Beanie. Sometimes this can lead to infinite recursion. To prevent this, or to decrease the database load, you can limit the nesting depth. By default, it is set to 3, which means it will fetch up to 3 levels of nested documents. You can configure:
Maximum:
Specific:
Find
You also can limit the nesting depth during find operations.
You can set up maximum depth for all linked documents:
Or you can set up depth for a specific field: