我正在寻找翻译来改变这一点:
getCollection('migrate').aggregate([
{ "$project": {
"Contrat": {"Field1":"$Field1", "Field2":"$Field2"},
"Formule": {"Field3":"$Field3", "Field4":"$Field4"}
}},
{ "$project": {
"Contrats": {"Contrat":"$Contrat", "Formule":"$Formule"}
}}
])
到 MongoJava 聚合框架。类似于:
AggregationOperation project = Aggregation.project("Field1,Field2"); // while naming it "Contrat"
AggregationOperation project2 = Aggregation.project("Field3,Fiel4"); // while naming it Formule
AggregationOperation project3 = Aggregation.project("Contrat,Formule"); // while naming it " Contrats"
AggregationOperation out = Aggregation.out("test");
Aggregation aggregation = Aggregation.newAggregation(project, project2, project3, out);
mongoTemplate.aggregate(aggregation, "<nameOfInitialCollection>", Class.class);
我在文档中找不到我的答案,我认为文档太差了,或者我可能太迷失在其中了(|愚蠢)。
我先谢谢你了。
最佳答案
您可以使用以下聚合。
AggregationOperation project = Aggregation.project().
and("Contrat").nested(Fields.fields("Field1","Field2")).
and("Formule").nested(Fields.fields("Field3","Field4"));
AggregationOperation project2 = Aggregation.project().
and("Contrats").nested(Fields.fields("Contrat","Formule")).
AggregationOperation out = Aggregation.out("test");
Aggregation aggregation = Aggregation.newAggregation(project, project2, out);
mongoTemplate.aggregate(aggregation, "<nameOfInitialCollection>", Class.class);
关于java - 项目作为 spring mongo 中的嵌套文档,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53213134/