嵌套数组上的 MongoDB $elemMatch 投影

Ed *_*goo 2 mongodb mongodb-query

我有一个这样的集合(摘要)。

    {
    "id":"summaryid",
    "locations": [
        {
            "id": "loc1",
            "datacenters": [
                {
                    "id": "dc1.1",
                    "clusters": [
                        {
                            "id": "cl1.1",
                            "servers": [
                                {
                                    "id": "srvr1.1",
                                    "services": [
                                        {
                                            "id": "srvc1.1"
                                        }
                                    ]
                                }
                            ]
                        }
                    ]
                },
                {
                    "id": "dc1.2",
                    "clusters": [
                        {
                            "id": "cl1.2",
                            "servers": [
                                {
                                    "id": "srvr1.2",
                                    "services": [
                                        {
                                            "id": "srvc1.2"
                                        }
                                    ]
                                }
                            ]
                        }
                    ]
                }
            ]
        },
        {
            "id": "loc2",
            "datacenters": [
                {
                    "id": "dc2.1",
                    "clusters": [
                        {
                            "id": "cl2.1",
                            "servers": [
                                {
                                    "id": "srvr2.1",
                                    "services": [
                                        {
                                            "id": "srvc2.1"
                                        }
                                    ]
                                }
                            ]
                        }
                    ]
                },
                {
                    "id": "dc2.2",
                    "clusters": [
                        {
                            "id": "cl2.2",
                            "servers": [
                                {
                                    "id": "srvr2.2",
                                    "services": [
                                        {
                                            "id": "srvc2.2"
                                        }
                                    ]
                                }
                            ]
                        }
                    ]
                }

            ]
        }
    ]
}
Run Code Online (Sandbox Code Playgroud)

现在我只想要 id 为 dc1.1 的数据中心的集群。我想排除集群的服务器。

我尝试使用带有 $elemMatch 和投影的查找查询,如下所示。

db.summary.find({}, {"locations": { $elemMatch: { "datacenters._id" : 
"dc1.1" } }, "locations.datacenters.clusters":0, 
"locations.datacenters.servers":0, "locations.datacentercount" : 0, 
"locations.clustercount" : 0, "locations.servercount" : 0}).pretty()
Run Code Online (Sandbox Code Playgroud)

我仍然获得所有数据中心,而不是仅 1 个与 ID 匹配的数据中心。我不确定我这样做是否正确。

谢谢你!

use*_*814 5

无法$elemMatch投影嵌套数组元素。

您可以在 3.4 服务器中尝试以下聚合。

使用$unwind几次到达嵌套数组并应用$match以选取嵌套数组元素。

   db.summary.aggregate([
      {
        "$match": {
          "locations.datacenters._id": "dc1.1"
        }
      },
      {
        "$unwind": "$locations"
      },
      {
        "$unwind": "$locations.datacenters"
      },
      {
        "$match": {
          "locations.datacenters._id": "dc1.1"
        }
      },
      {
        "$project": {
          "locations.datacenters.clusters.servers": 0
        }
      }
    ])
Run Code Online (Sandbox Code Playgroud)

{"$project": {"locations.datacenters.clusters.servers": 0}}将删除该servers字段,同时保留最终输出中的所有其他字段。

来自文档

如果指定排除 _id 以外的字段,则不能使用任何其他 $project 规范形式:即,如果排除字段,则也不能指定包含字段、重置现有字段的值或添加新字段。

参考: https ://docs.mongodb.com/manual/reference/operator/aggregation/project/#exclude-fields