使用 MongoTemplate 聚合过滤 MongoDB 中的嵌套元素
Filter nested elements in MongoDB using MongoTemplate Aggregation
我正在尝试过滤标题与给定输入标题数组匹配的字段数组,并显示完整文档,不包括不匹配的 fields.I 具有以下文档。实际上,我想使用 MongoTemplate 来实现这一点,但首先我试图通过 mongo 查询来获取它。下面是我的文档:
{
"version": 2,
"pageName": "Content_2",
"domain": "bingo.com",
"locale": "en-us",
"contents": [
{
"contentName": "Template_2",
"fields": [
{
"fieldType": "Plain Text",
"id": "companyName456",
"title": "Company Name",
"alternateText": "Company Name",
"value": "Microsoft",
"placeholder": "Enter your Company name"
},
{
"fieldType": "Plain Text",
"id": "designation789",
"title": "Designation",
"alternateText": "Designation",
"value": "Software Engineer",
"placeholder": "Enter your designation name"
}
]
}
]
}
我尝试了以下查询,但它返回的是空结果:
db.contents.aggregate(
[
{ $match: { locale: "en-us" } },
{
$redact: {
$cond: {
if: { $in: [ "$title", ["Designation"] ] },
then: "$$DESCEND",
else: "$$PRUNE"
}
}
}
]
);
我期待以下结果:
{
"pageName": "Home",
"link": "hello.com",
"locale": "en-us",
"contents": [
{
"contentName": "Template_2",
"fields": [
{
"fieldType": "Plain Text",
"id": "designation789",
"title": "Designation",
"alternateText": "Designation",
"value": "Software Engineer",
"placeholder": "Enter your designation name"
}
]
}
]
}
请指导。我对 MongoDB 很陌生。
像这样的东西应该可以工作
db.contents.aggregate([
# Match that local
{$match: { locale: "en-us" } },
# Unwind by contents and contents.fields
{$unwind: "$contents"},
{$unwind: "$contents.fields"},
# Match them
{$match: { "contents.fields.title": "Designation" } },
# group back by _id of the document
{$group: {
"_id": "$_id",
"contents": { "$push": "$contents" }
}}
])
如果您需要所有其他字段也按它们分组,例如
db.contents.aggregate([
{$match: { locale: "en-us" } },
{$unwind: "$contents"},
{$unwind: "$contents.fields"},
{$match: { "contents.fields.title": "Designation" } },
{$group: {
"_id": {
"_id": "$_id",
"version": "$version",
"pageName": "$pageName",
"domain": "$domain",
"locale": "$locale",
},
"contents": { "$push": "$contents" }
}},
{$project: {"version": "$_id.version",
"_id": "$_id._id",
"pageName": "$_id.pageName",
"locale": "$_id.locale",
"domain": "$_id.domain",
"contents": "$contents",}}
])
我正在尝试过滤标题与给定输入标题数组匹配的字段数组,并显示完整文档,不包括不匹配的 fields.I 具有以下文档。实际上,我想使用 MongoTemplate 来实现这一点,但首先我试图通过 mongo 查询来获取它。下面是我的文档:
{
"version": 2,
"pageName": "Content_2",
"domain": "bingo.com",
"locale": "en-us",
"contents": [
{
"contentName": "Template_2",
"fields": [
{
"fieldType": "Plain Text",
"id": "companyName456",
"title": "Company Name",
"alternateText": "Company Name",
"value": "Microsoft",
"placeholder": "Enter your Company name"
},
{
"fieldType": "Plain Text",
"id": "designation789",
"title": "Designation",
"alternateText": "Designation",
"value": "Software Engineer",
"placeholder": "Enter your designation name"
}
]
}
]
}
我尝试了以下查询,但它返回的是空结果:
db.contents.aggregate(
[
{ $match: { locale: "en-us" } },
{
$redact: {
$cond: {
if: { $in: [ "$title", ["Designation"] ] },
then: "$$DESCEND",
else: "$$PRUNE"
}
}
}
]
);
我期待以下结果:
{
"pageName": "Home",
"link": "hello.com",
"locale": "en-us",
"contents": [
{
"contentName": "Template_2",
"fields": [
{
"fieldType": "Plain Text",
"id": "designation789",
"title": "Designation",
"alternateText": "Designation",
"value": "Software Engineer",
"placeholder": "Enter your designation name"
}
]
}
]
}
请指导。我对 MongoDB 很陌生。
像这样的东西应该可以工作
db.contents.aggregate([
# Match that local
{$match: { locale: "en-us" } },
# Unwind by contents and contents.fields
{$unwind: "$contents"},
{$unwind: "$contents.fields"},
# Match them
{$match: { "contents.fields.title": "Designation" } },
# group back by _id of the document
{$group: {
"_id": "$_id",
"contents": { "$push": "$contents" }
}}
])
如果您需要所有其他字段也按它们分组,例如
db.contents.aggregate([
{$match: { locale: "en-us" } },
{$unwind: "$contents"},
{$unwind: "$contents.fields"},
{$match: { "contents.fields.title": "Designation" } },
{$group: {
"_id": {
"_id": "$_id",
"version": "$version",
"pageName": "$pageName",
"domain": "$domain",
"locale": "$locale",
},
"contents": { "$push": "$contents" }
}},
{$project: {"version": "$_id.version",
"_id": "$_id._id",
"pageName": "$_id.pageName",
"locale": "$_id.locale",
"domain": "$_id.domain",
"contents": "$contents",}}
])