【发布时间】:2017-10-10 15:46:03
【问题描述】:
我有一个这样的集合(摘要)。
{
"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 和投影的 find 查询,如下所示。
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 匹配。 我不确定我这样做是否正确。
谢谢!
【问题讨论】:
-
你能发布你预期的 json 结果吗?你只希望 { "id": "dc1.1", "clusters": [ {"id": "cl1.1",} ] }
-
您的
$elemMatch指令使用的是"datacenters._id",但正确的路径(基于您提供的示例文档)是"datacenters.id" -
find是查找文档,而不是文档的一部分。投影无条件适用于所有属性,在这里不是正确的工具。您需要使用聚合来检索子文档。 -
谢谢亚历克斯。我尝试了聚合并得到了我想要的。以下是我正在使用的。 ` db.summary.aggregate([ {"$unwind": "$locations"}, {"$unwind": "$locations.datacenters"}, { $match: { "locations.datacenters._id" : "dc1. 1" } }, { $project : {"locations.datacenters.servers" : 0, "locations.datacenters.clusters.servers" : 0} } ]).pretty() `
-
新的挑战是我正在使用 spring-mongodb-data 的 ProjectionOperation 和 mongoTemplate。我不断收到以下错误。
ProjectionOperation projectStage = new ProjectionOperation().andExclude("locations.datacenters.servers", "locations.datacenters.clusters.servers");Exclusion of field locations.datacenters.servers not allowed. Projections by the mongodb aggregation framework only support the exclusion of the _id field!
标签: mongodb mongodb-query