【发布时间】:2021-01-16 03:36:33
【问题描述】:
我在 MongoDB 中有一些文档,其中的其他嵌套文档都带有“活动”字段。 例如:
{
"id": "PRODUCT1",
"name": "Product 1",
"active": true,
"categories": [
{
"id": "CAT-1",
"active": true,
"subcategories": [
{
"id": "SUBCAT-1",
"active": false
},
{
"id": "SUBCAT-2",
"active": true
}
]
},
{
"id": "CAT-2",
"active": false,
"subcategories": [
{
"id": "SUBCAT-3",
"active": true
}
]
}
]
}
有没有办法找到所有文档但只保留“活动”嵌套文档。
这是我想要的结果:
{
"id": "PRODUCT1",
"name": "Product 1",
"active": true,
"categories": [
{
"id": "CAT-1",
"active": true,
"subcategories": [
{
"id": "SUBCAT-2",
"active": true
}
]
}
]
}
事先知道我不知道文档架构。这就是为什么我需要一种条件通配符投影...(即*.active=true)。这是可能的还是必须在服务器端完成?
【问题讨论】:
标签: mongodb mongodb-query spring-data-mongodb