mirror of
https://github.com/FerretDB/documentdb.git
synced 2026-11-07 03:22:46 -05:00
* Add doc site * update deploy workflow * add gateway doc * update structure and add gateway doc * update missing image --------- Co-authored-by: sajeetharan <sajeetharan@outlook.com>
1.5 KiB
1.5 KiB
Perform aggregations Group by
DocumentDB provides the documentdb_api.aggregate_cursor_first_page function, for performing aggregations over the document store.
The example projects an aggregation on number of patients registered over the years.
SELECT cursorpage FROM documentdb_api.aggregate_cursor_first_page('documentdb', '{ "aggregate": "patient", "pipeline": [ { "$group": { "_id": "$registration_year", "count_patients": { "$count": {} } } } ] , "cursor": { "batchSize": 3 } }');
We can perform more complex operations, listing below a few more usage examples. The example demonstrates an aggregation on patients, categorizing them into buckets defined by registration_year boundaries.
SELECT cursorpage FROM documentdb_api.aggregate_cursor_first_page('documentdb', '{ "aggregate": "patient", "pipeline": [ { "$bucket": { "groupBy": "$registration_year", "boundaries": ["2022","2023","2024"], "default": "unknown" } } ], "cursor": { "batchSize": 3 } }');
This query performs an aggregation on the patient collection to group documents by registration_year. It collects unique patient conditions for each registration year using the $addToSet operator.
SELECT cursorpage FROM documentdb_api.aggregate_cursor_first_page('documentdb', '{ "aggregate": "patient", "pipeline": [ { "$group": { "_id": "$registration_year", "conditions": { "$addToSet": { "conditions" : "$conditions" } } } } ], "cursor": { "batchSize": 3 } }');