Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
87cbc52
commit 702b560
Showing
17 changed files
with
238 additions
and
173 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,64 +1,39 @@ | ||
# Aggregations | ||
|
||
[AggregationQuery](https://roman-right.github.io/beanie/api/queries/#aggregationquery) is used to aggregate data | ||
over the whole collection or the subset selected with | ||
the [FindMany](https://roman-right.github.io/beanie/api/queries/#findmany) query. | ||
You can aggregate and over the whole collection, using `aggregate()` method of the `Document` class, and over search criteria, using `FindMany` instance. | ||
|
||
## Preset aggregations | ||
#### Aggregation Methods | ||
|
||
[AggregateMethods](https://roman-right.github.io/beanie/api/interfaces/#aggregatemethods) is a list of preset | ||
aggregations, which simplifies some use cases. | ||
`FindMany` and `Document` classes implements [AggregateMethods](https://roman-right.github.io/beanie/api/interfaces/#aggregatemethods) interface with preset methods | ||
|
||
```python | ||
class Sample(Document): | ||
category: str | ||
price: int | ||
count: int | ||
|
||
|
||
sum_count = await Sample.find(Sample.price <= 100).sum(Sample.count) | ||
Example of average calculation: | ||
|
||
# Or for the whole collection: | ||
|
||
avg_price = await Sample.avg(Sample.count) | ||
*With search criteria* | ||
```python | ||
avg_price = await Product.find( | ||
Product.category.name == "Chocolate" | ||
).avg(Product.price) | ||
``` | ||
|
||
*Over the whole collection* | ||
```python | ||
avg_price = await Product.avg(Product.price) | ||
``` | ||
|
||
## Aggregate over collection | ||
#### Native syntax | ||
|
||
`AggregationQuery` implements async generator pattern - results | ||
are available via `async for` loop | ||
You can use the native PyMongo syntax of the aggregation pipelines to aggregate over the whole collection or over the subset too. `projection_model` parameter is responsible for the output format. It will return dictionaries, if this parameter is not provided. | ||
|
||
```python | ||
class OutputItem(BaseModel): | ||
id: str = Field(None, alias="_id") | ||
total: int | ||
|
||
|
||
async for item in Sample.aggregate( | ||
[{"$group": {"_id": "$category", "total": {"$sum": "$count"}}}], | ||
aggregation_model=OutputItem, | ||
): | ||
... | ||
``` | ||
|
||
or with `to_list` method: | ||
|
||
```python | ||
result = await Sample.aggregate( | ||
[{"$group": {"_id": "$category", "total": {"$sum": "$count"}}}] | ||
result = await Product.find( | ||
Product.category.name == "Chocolate").aggregate( | ||
[{"$group": {"_id": "$category.name", "total": {"$avg": "$price"}}}], | ||
projection_model=OutputItem | ||
).to_list() | ||
``` | ||
|
||
If the `aggregation_model` parameter is not set, it will return dicts. | ||
|
||
## Over subsets | ||
|
||
To aggregate over a specific subset, FindQuery could be used. | ||
|
||
```python | ||
result = await Sample.find(Sample.price < 10).aggregate( | ||
[{"$group": {"_id": "$category", "total": {"$sum": "$count"}}}] | ||
).to_list() | ||
``` | ||
|
||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,7 @@ | ||
# Delete documents | ||
|
||
Beanie supports, and single, and batch deletions: | ||
|
||
## Single | ||
|
||
```python | ||
|
Oops, something went wrong.