This question already has answers here:
Find in Double Nested Array MongoDB

(2个答案)


9个月前关闭。





我正在尝试使用以下查询过滤文档:

db.projects.aggregate([
    {
        $project: {
            deployments: {
                $filter: {
                    input: "$releases.deployments",
                    as: "deployment",
                    cond: { $eq: ["$$deployment.environment", "Live"] }
                }
            }
        }
    }
])


deployments的输出始终是一个空数组,尽管如果将条件更改为$ne则它将返回所有结果。

如何获取过滤条件以仅返回deployment.environment等于字符串Live的记录?

这是json的样本片段:

{
      "project_id": "1",
      "project_group": "A",
      "releases": [
        {
          "version": "1",
          "deployments": [
            {
              "environment": "Integration",
              "created": "2019-10-01T06:40:01.000Z",
              "state": "Success",
              "name": "Deploy to Integration"
            },
            {
              "environment": "Test",
              "created": "2019-10-01T08:23:58.000Z",
              "state": "Success",
              "name": "Deploy to Test"
            },
            {
              "environment": "Live",
              "created": "2019-10-01T09:02:17.000Z",
              "state": "Success",
              "name": "Deploy to Live"
            }
          ]
        }
      ]
    }

最佳答案

如果releases是嵌入式文档,但它是嵌入式文档的数组,则查询将完美工作。

以下查询将遍历releases的每个元素,并过滤环境为Live的部署。

db.collection.aggregate([
    {
        $project:{
            "releases":{
                $map:{
                    "input":"$releases",
                    "as":"release",
                    "in":{
                        $mergeObjects:[
                            "$$release",
                            {
                                "deployments":{
                                    $filter:{
                                        "input":"$$release.deployments",
                                        "as":"deployment",
                                        "cond":{
                                            $eq:["$$deployment.environment","Live"]
                                        }
                                    }
                                }
                            }
                        ]
                    }
                }
            }
        }
    },
    {
        $project:{
            "releases":{
                $filter:{
                    "input":"$releases",
                    "as":"release",
                    "cond":{
                        $ne:["$$release.deployments.0",null]
                    }
                }
            }
        }
    }
]).pretty()


输出:

{
    "_id" : ObjectId("5d93401ef2e6411a68a145ee"),
    "releases" : [
        {
            "version" : "1",
            "deployments" : [
                {
                    "environment" : "Live",
                    "created" : "2019-10-01T09:02:17.000Z",
                    "state" : "Success",
                    "name" : "Deploy to Live"
                }
            ]
        }
    ]
}

关于mongodb - Mongo聚合和过滤器,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/58184287/

10-16 21:23