【发布时间】:2019-09-06 02:08:28
【问题描述】:
我编写了一个 MongoDB 本机查询,它按预期工作。我正在尝试编写其 Spring 数据等效查询,但无法这样做。基本上,我想根据输入数组中提供的标题过滤掉字段数组。这是示例文档:
{
"domain":"diageotest.com",
"locale":"en-gb",
"pageName":"Content_1",
"contents":[
{
"contentName":"Template_1",
"fields":[
{
"id":"firstname",
"fieldType":"Plain Text",
"title":"First Name",
"value":"Pawan"
},
{
"id":"lastname",
"fieldType":"Plain Text",
"title":"Last Name",
"value":"Kumar"
}
]
}
],
"createdBy":"ag-KumarJog",
"createdDate":"Jan 23,2019",
"isDefaultLocale":true,
"modifiedBy":"ag-KumarJog",
"modifiedDate":"Jan 23,2019",
"version":1
}
下面是我的原生查询:
db.contents.aggregate([
{$match: { $and: [
{"domain": "diageotest.com"},
{"locale": {$in: ["en-in", "en-us"]}},
{"contents.contentName": "Template_1"}
]
}
},
{$unwind: "$contents"},
{$unwind: "$contents.fields"},
{$match: { "contents.fields.title": {$in: ["First Name"]}}},
{$group: { "_id": "$_id",
"contents": { "$push": "$contents"},
"root": {$first:"$$ROOT"} }},
{$replaceRoot:{newRoot:{$mergeObjects:["$root",{contents:'$contents'}]}}},
{$skip : 0},
{$limit : 5}
]);
在这里,在上面的查询中,如果我在数组中传递“名字”($in 条件),那么输出 JSON 将只包含标题为“名字”的字段。
在 java 中,我在下面尝试但卡在替换根目录:
MatchOperation matchOperation = this.matchByTemplateDomainAndLocales(domainName, templateName,
requestBody.getLocales());
// ---------------------------------------------------//
UnwindOperation unwindContents = unwind("$contents");
UnwindOperation unwindFields = unwind("$contents.fields");
MatchOperation matchFields = match(where("contents.fields.title").in(requestBody.getFields()));
GroupOperation groupOperation = group("id").push("$contents").as("contents").first("$$ROOT").as("root");
//ReplaceRootOperation replaceRootOperation = builder().withDocument()
// ---------------------------------------------------//
SortOperation sortOperation = sort(sort);
LimitOperation limitOperation = limit(requestBody.getLimit());
SkipOperation skipOperation = skip(requestBody.getSkip().longValue());
我无法在替换根中找到合适的方法来接受数组并执行合并。请帮忙。
【问题讨论】:
标签: mongodb spring-boot spring-data aggregation-framework