【发布时间】:2019-10-19 20:13:00
【问题描述】:
我需要使用 mongoose nodejs 聚合多个嵌套对象来过滤多个嵌套数组对象。我已经提到了来自集合和多过滤器对象的示例记录数据。
我使用一个电子商务产品列表视图页面。在此列表页面中,我们有许多具有不同功能的产品。在这里,我需要使用该产品功能列表过滤该产品。
示例:
我有 3 部具有不同功能的手机,例如 颜色,内存,存储。这些功能被放置在“productfilter”中 下面的样本收集记录中的数组。我可以选择多个 过滤手机的功能,例如,我选择颜色:蓝色、金色和 内存:6GB、8GB。这里来自示例产品列表数据的第二个产品,需要 根据选定的过滤器获取。因为颜色:蓝色和内存:6GB 是 与第二个产品对象中的“IsDefault:'true'”匹配 示例产品列表数据
这里需要根据过滤请求数据过滤记录,同时需要检查“isDefault”是否为“true”
样本采集记录
[
{
"_id": "5d710f9950b94f237f04407b",
"productfilter": [
{
"_id": "5d8d29ae014da910c0de0799",
"specTitle": "Color",
"attributeOptions": [
{
"_id": "5d8d29ae014da910c0de079d",
"specLabel": "Rose Gold",
"isDefault": "false",
"specContent": "mobiles-accessories-mobiles-redmi-redmi-redmi-note-6-pro-rose-gold-6gb-64gb-redmi-note-6-pro"
},
{
"_id": "5d8d29ae014da910c0de079c",
"specLabel": "Black",
"isDefault": "true",
"specContent": "mobiles-accessories-mobiles-redmi-redmi-redmi-note-6-pro-black-6gb-64gb-redmi-note-6-pro"
},
{
"_id": "5d8d29ae014da910c0de079b",
"specLabel": "Blue",
"isDefault": "false",
"specContent": "mobiles-accessories-mobiles-redmi-redmi-redmi-note-6-pro-blue-6gb-64gb-redmi-note-6-pro"
},
{
"_id": "5d8d29ae014da910c0de079a",
"specLabel": "Red",
"isDefault": "false",
"specContent": "mobiles-accessories-mobiles-redmi-redmi-redmi-note-6-pro-red-6gb-64gb-redmi-note-6-pro"
}
]
},
{
"_id": "5d8d29ae014da910c0de0796",
"specTitle": "RAM",
"attributeOptions": [
{
"_id": "5d8d29ae014da910c0de0798",
"specLabel": "4GB",
"isDefault": "false",
"specContent": "mobiles-accessories-mobiles-redmi-redmi-redmi-note-6-pro-black-4gb-64gb-redmi-note-6-pro"
},
{
"_id": "5d8d29ae014da910c0de0797",
"specLabel": "6GB",
"isDefault": "true",
"specContent": "mobiles-accessories-mobiles-redmi-redmi-redmi-note-6-pro-black-6gb-64gb-redmi-note-6-pro"
}
]
},
{
"_id": "5d8d29ae014da910c0de0794",
"specTitle": "Storage",
"attributeOptions": [
{
"_id": "5d8d29ae014da910c0de0795",
"specLabel": "64GB",
"isDefault": "true",
"specContent": ""
}
]
}
]
},
{
"_id": "5d710fe050b94f237f04407e",
"productfilter": [
{
"_id": "5d8d29ae014da910c0de07a3",
"specTitle": "Color",
"attributeOptions": [
{
"_id": "5d8d29ae014da910c0de07a7",
"specLabel": "Rose Gold",
"isDefault": "false",
"specContent": "mobiles-accessories-mobiles-redmi-redmi-redmi-note-6-pro-rose-gold-6gb-64gb-redmi-note-6-pro"
},
{
"_id": "5d8d29ae014da910c0de07a6",
"specLabel": "Black",
"isDefault": "false",
"specContent": "mobiles-accessories-mobiles-redmi-redmi-redmi-note-6-pro-black-6gb-64gb-redmi-note-6-pro"
},
{
"_id": "5d8d29ae014da910c0de07a5",
"specLabel": "Blue",
"isDefault": "true",
"specContent": "mobiles-accessories-mobiles-redmi-redmi-redmi-note-6-pro-blue-6gb-64gb-redmi-note-6-pro"
},
{
"_id": "5d8d29ae014da910c0de07a4",
"specLabel": "Red",
"isDefault": "false",
"specContent": "mobiles-accessories-mobiles-redmi-redmi-redmi-note-6-pro-red-6gb-64gb-redmi-note-6-pro"
}
]
},
{
"_id": "5d8d29ae014da910c0de07a0",
"specTitle": "RAM",
"attributeOptions": [
{
"_id": "5d8d29ae014da910c0de07a2",
"specLabel": "4GB",
"isDefault": "false",
"specContent": "mobiles-accessories-mobiles-redmi-redmi-redmi-note-6-pro-blue-4gb-64gb-redmi-note-6-pro"
},
{
"_id": "5d8d29ae014da910c0de07a1",
"specLabel": "6GB",
"isDefault": "true",
"specContent": "mobiles-accessories-mobiles-redmi-redmi-redmi-note-6-pro-blue-6gb-64gb-redmi-note-6-pro"
}
]
},
{
"_id": "5d8d29ae014da910c0de079e",
"specTitle": "Storage",
"attributeOptions": [
{
"_id": "5d8d29ae014da910c0de079f",
"specLabel": "64GB",
"isDefault": "true",
"specContent": ""
}
]
}
]
}
]
从产品列表视图页面中的过滤选项列表中采样选定的请求数据,以过滤上述记录
{
"filter": {
"genericFilter": [
{
"specTitle": "Color",
"specLabel": [
"Blue",
"Gold",
"Rose Gold"
]
},
{
"specTitle": "RAM",
"specLabel": [
"6GB",
"8GB"
]
},
{
"specTitle": "Storage",
"specLabel": [
"64GB",
"256GB"
]
}
]
}
}
【问题讨论】:
-
嗨@uday-s 这不是很清楚。提供更好的描述
-
我已经更新了详细描述。你能检查一下吗
标签: node.js mongodb mongoose mongodb-query aggregation-framework