【发布时间】:2021-04-24 11:44:45
【问题描述】:
我想创建一种过滤体验,我从请求参数中获取过滤条件,并使用 mongo 聚合框架 过滤数据。请建议我一个合适的方法。
让数据的集合像
{
"material": "silver",
"type": "beads",
"shape": "round",
"_user": "5f9b78bf1130ca88e2e1d"
},
{
"material": "mixed",
"type": "jewels",
"shape": "round",
"_user": "5f9b78bf1130ca88e2e1d"
},
{
"material": "gold",
"type": "jewels",
"shape": "other",
"_user": "5fda2b717112437f9c06"
},
{
"material": "silver",
"type": "jewels",
"shape": "other",
"_user": "5f9b78bf1130ca88e2e1d"
},
{
"material": "mixed",
"type": "jewels",
"shape": "other",
"_user": "5fda2b717112437f9c706"
},
让查询的数据为 _user 等于 5f9b78bf1130ca88e2e1d, material 等于 silver, mixed 的数组, type 等于 珠子、珠宝 的数组 & shape 等于 round
的数组我的查询应该返回每个过滤器类别(用户、材料、形状、类型)中的后续计数。
data = {
categorizedByUser: [
{
_id: '5f9b78bf1130ca88e2e1d',
count: 2
},
{
_id: '5fda2b717112437f9c06',
count: 0
},
],
categorizedByMaterial: [
{
_id: 'silver',
count: 1
},
{
_id: 'mixed',
count: 1
},
{
_id: 'gold',
count: 0
},
],
categorizedByShape: [
{
_id: 'round',
count: 2
},
{
_id: 'other',
count: 0
},
]
categorizedByType: [
{
_id: 'beads',
count: 1
},
{
_id: 'jewels',
count: 1
},
]
]
【问题讨论】:
标签: mongodb aggregation-framework filtering dsa