【发布时间】:2021-05-22 00:21:25
【问题描述】:
我无法理解如何在 Mongo 中使用聚合管道。
给定以下文件列表:
db.dishes.insertMany([
{_id: "Vanilla Sundae", keywords: ["vanilla", "ice cream", "desert"] },
{_id: "Vanilla Cake", keywords: ["vanilla", "cake", "baking", "desert"] },
{_id: "Chocolate Cake", keywords: ["chocolate", "cake", "baking", "desert"] }
])
如何创建一个聚合,该聚合将返回 keywords 的不同关键字列表和文档计数:
[
{"_id": "vanilla", "count": 2},
{"_id": "ice cream", "count": 1},
{"_id": "desert", "count": 3},
{"_id": "baking", "count": 2},
{"_id": "cake", "count": 2},
{"_id": "chocolate", "count": 1}
]
【问题讨论】:
-
这个答案对你有帮助吗?