我正在使用Java和Springboot.是否可以从json文档中检索嵌套json字段


{
"_id" : ObjectId("dde3431134247d401b1cef"),
"_resourceId" : "fwf4-fefre4-ffdfwsc",
"organizationId" : 343203,
"domains" : [
{
"_resourceId" : "da7-cwcwe-2432d",
"name" : "d12.net",
"tenantId" : "A1650",
"application" : "TEST",
"activeInd" : true,
"subdomains" : [
{
"_resourceId" : "fw243-weded3-2eddas",
"name" : "name1",
"clientName" : "Andrew",
"phoneNumber" : "8573458456",
"email" : "modalwindow@gmail.com",
},
{
"_resourceId" : "bce3-cwdd32ede-23ede",
"name" : "name2",
"clientName" : "client2",
"phoneNumber" : "9999999999",
"email" : "test@gmail.com",
}
]
}
]
}

我使用Springboot和MongoTemplate查找查询。如果我想检索基于"domains.subdomain.name"字段的子域,我可以从mongo查询中只获得子域json,或者我可以获得整个文档,然后在java代码中迭代和过滤子域。

先使用$unwind,然后使用$replaceWith

db.collection.aggregate([
{
$match: {
"domains.subdomains.name": "name1"
}
},
{
$unwind: "$domains"
},
{
$unwind: "$domains.subdomains"
},
{
$match: {
"domains.subdomains.name": "name1"
}
},
{
$replaceWith: "$domains.subdomains"
}
])

mongoplayground

相关内容

  • 没有找到相关文章

最新更新