mongodb - Mongodb 使用带有嵌套数组的 $filter
问题描述
我有这个文件
[
{
"id" : 1,
"prices" : [
{
"value": 50,
"term": [
{
"term_value" : 6
},
{
"term_value" : 12
}
]
},
{
"value": 100,
"term": [
{
"term_value" : 18
}
]
}
]
},
{
"id" : 2,
"prices" : [
{
"value": 50,
"term": [
{
"term_value" : 6
},
{
"term_value" : 18
}
]
},
{
"value": 150,
"term": [
{
"term_value" : 24
}
]
}
]
}
]
我想使用聚合框架来获取至少有一个prices.term.value
等于的文档6
我正在使用这个
Product.aggregate(
[
{ "$match": { "prices.term.value": {"$eq": 6} } },
]
)
它返回所有文档,因为所有文档都至少有一个术语满足条件,没有问题
我需要的是,使用 $filter 只返回至少有一个条件满足条件的价格
这是我需要的虚拟代码
{
"$addFields": {
"prices": {
"$filter": {
"input": "$prices",
"as": "field0",
"cond": {
"$eq": [
"$$field0.term.term_value",
6
]
}
}
}
}
}
但它也不工作,有什么建议吗?
我需要的结果是
[
{
"id" : 1,
"prices" : [
{
"value": 50,
"term": [
{
"term_value" : 6
},
{
"term_value" : 12
}
]
},
]
},
{
"id" : 2,
"prices" : [
{
"value": 50,
"term": [
{
"term_value" : 6
},
{
"term_value" : 18
}
]
},
]
}
]
谢谢
解决方案
推荐阅读
- c# - 仅在参考项目检查中使用的 C# 类?
- c++ - 图上的深度优先搜索算法中的内存泄漏
- mysql - 无法重新初始化 Metastore
- python - Python:要映射的键值对中的字符串列表
- excel - 多个模块和子程序之间的错误处理
- python - tkinter Checkbutton - 如何在应用程序开始时创建选中按钮?
- php - 如何使用 nginx 别名为 php-fpm 设置正确的根目录?
- python - sklearn.externals joblib 抛出 ImportError - 服务器上没有名为 joblib.numpy_pickle 的模块
- reactjs - 监控 Firebase 身份验证对象属性更改?
- c++ - std:string::substr 是异常安全的吗?