Mongo聚合和过滤

时间:2019-10-01 11:55:07

标签: mongodb aggregation

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

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"
            }
          ]
        }
      ]
    }

2 个答案:

答案 0 :(得分:2)

如果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"
                }
            ]
        }
    ]
}

答案 1 :(得分:0)

尝试在下面进行汇总:

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