英文:
MongoDB filter nested array
问题
[
  {
    "_id": 1,
    "title": "dummy title",
    "settings": [
      {
        "type": "light",
        "status": "enabled"
      },
      {
        "type": "toolbar",
        "status": "enabled"
      }
    ]
  }
]
我想按ID获取它,但只想获取“enabled”设置,不确定聚合管道应该是什么样子的。
我应该使用Java / Kotlin中的mongoTemplate创建查询,但仅有Mongo查询即可。
英文:
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"
      }
    ]
  }
]
I wanna fetch it by Id but only with the "enabled" settings and not sure how the aggregation pipeline should look like.
I supposed to create the query using mongoTemplate in Java / Kotlin but even just the mongo query would be enough.
答案1
得分: 1
// MongoDB shell/CLI版本4.26的工作代码(在Windows 10机器/操作系统上)
> db.titles.find().pretty();
{
        "_id" : 1,
        "title" : "虚拟标题",
        "settings" : [
                {
                        "type" : "light",
                        "status" : "已启用"
                },
                {
                        "type" : "flare",
                        "status" : "已禁用"
                },
                {
                        "type" : "toolbar",
                        "status" : "已启用"
                }
        ]
}
> db.titles.aggregate([
... {$unwind:"$settings"},
... {$match:{"settings.status":"已启用"}}
... ]);
{ "_id" : 1, "title" : "虚拟标题", "settings" : { "type" : "light", "status" : "已启用" } }
{ "_id" : 1, "title" : "虚拟标题", "settings" : { "type" : "toolbar", "status" : "已启用" } }
> print("MongoDB: ",db.version());
MongoDB:  4.2.6
>
英文:
//working code from MongoDB shell/CLI version 4.26(on windows 10 machine/OS)
> db.titles.find().pretty();
{
        "_id" : 1,
        "title" : "dummy title",
        "settings" : [
                {
                        "type" : "light",
                        "status" : "enabled"
                },
                {
                        "type" : "flare",
                        "status" : "disabled"
                },
                {
                        "type" : "toolbar",
                        "status" : "enabled"
                }
        ]
}
> db.titles.aggregate([
... {$unwind:"$settings"},
... {$match:{"settings.status":"enabled"}}
... ]);
{ "_id" : 1, "title" : "dummy title", "settings" : { "type" : "light", "status" : "enabled" } }
{ "_id" : 1, "title" : "dummy title", "settings" : { "type" : "toolbar", "status" : "enabled" } }
> print("MongoDB: ",db.version());
MongoDB:  4.2.6
>
答案2
得分: 1
你可以使用$filter来实现。
db.collection.aggregate([
  {
    $project: {
      _id: 1,
      title: 1,
      settings: {
        $filter: {
          input: "$settings",
          as: "item",
          cond: {
            $eq: [
              "$$item.status",
              "enabled"
            ]
          }
        }
      }
    }
  }
])
在此链接中尝试。
英文:
You can do it with a $filter
db.collection.aggregate([
  {
    $project: {
      _id: 1,
      title: 1,
      settings: {
        $filter: {
          input: "$settings",
          as: "item",
          cond: {
            $eq: [
              "$$item.status",
              "enabled"
            ]
          }
        }
      }
    }
  }
])
try it here
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。


评论