我有一个这样的集合(摘要)。
{
"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"
}
]
}
]
}
]
}
]
}
]
}
现在我只需要 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()
我仍然获得所有数据中心,而不是仅 1 个与 ID 匹配的数据中心。
我不确定我这样做是否正确。
谢谢你!