# NoSQL (MongoDB) - ドキュメント指向 ## Aggregation Pipeline (集計パイプライン) - [Aggregation Pipeline - MongoDB Manual v8.0](https://www.mongodb.com/docs/manual/core/aggregation-pipeline/) An aggregation pipeline consists of one or more [stages](https://www.mongodb.com/docs/manual/reference/operator/aggregation-pipeline/#std-label-aggregation-pipeline-operator-reference) that process documents: - Each stage performs an operation on the input documents. For example, a stage can filter documents, group documents, and calculate values. - The documents that are output from a stage are passed to the next stage. - An aggregation pipeline can return results for groups of documents. For example, return the total, average, maximum, and minimum values. ## 例 ```json { "member_id": 1, "custom_fields": { "職務スキル": "Java", "プロジェクト経験": "3年以上" } } ``` ## クロス集計 ```js db.members.aggregate([ { $group: { _id: { skill: "$custom_fields.職務スキル", experience: "$custom_fields.プロジェクト経験" }, count: { $sum: 1 } } }, { $sort: { "_id.skill": 1, "_id.experience": 1 } } ]) ``` | 職務スキル | プロジェクト経験 | count | | ---------- | ---------------- | ----- | | Java | 3年以上 | 5 | | Java | 1-3年 | 8 | | Python | 3年以上 | 4 | | Python | 1-3年 | 6 | ### Pros and Cons - **Pros**: 柔軟なスキーマ、`aggregation pipeline`による強力な集計機能、分散型スケーラビリティ - **Cons**: ドキュメント内にデータを持つため、頻繁に更新がある場合は管理が複雑になる可能性