MongoDB过滤嵌套数组

huangapple go评论90阅读模式
英文:

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

huangapple
  • 本文由 发表于 2020年10月12日 00:03:31
  • 转载请务必保留本文链接:https://go.coder-hub.com/64306166.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定