MongoDB过滤器嵌套数组 [英] MongoDB filter nested array
本文介绍了MongoDB过滤器嵌套数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一个看起来像这样的收藏集:
I have a collection that looks something like:
[
{
"_id": 1,
"title": "dummy title",
"settings": [
{
"type": "light",
"status": "enabled"
},
{
"type": "flare",
"status": "disabled"
},
{
"type": "toolbar",
"status": "enabled"
}
]
}
]
我想通过ID来获取它,但只能使用已启用"设置,不确定聚合管道的外观.
I wanna fetch it by Id but only with the "enabled" settings and not sure how the aggregation pipeline should look like.
我应该使用Java/Kotlin中的mongoTemplate创建查询,但是即使仅mongo查询也足够.
I supposed to create the query using mongoTemplate in Java / Kotlin but even just the mongo query would be enough.
推荐答案
您可以使用$filter
db.collection.aggregate([
{
$project: {
_id: 1,
title: 1,
settings: {
$filter: {
input: "$settings",
as: "item",
cond: {
$eq: [
"$$item.status",
"enabled"
]
}
}
}
}
}
])
在此处尝试
这篇关于MongoDB过滤器嵌套数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文