我希望将这个片段复制到 Java 代码中:
\n\ndb.getCollection(\'admins_comptes_client_ceov4\').aggregate([\n{$lookup: {from: "contrats_ceov4",localField: "CUSTOMERNUMBER",foreignField: "CUSTOMERNUMBER",as: "arrayForeignObject"}\n{$unwind: { path: "$arrayForeignObject", preserveNullAndEmptyArrays: true}},\n{$replaceRoot: { newRoot: { $mergeObjects: [ "$arrayForeignObject", "$$ROOT" ] } }},\n{ $project: { "arrayForeignObject": 0, "_id": 0\xc2\xa0} },\n{ $out: "aggregate" }])\nRun Code Online (Sandbox Code Playgroud)\n\n到目前为止我在这里:
\n\n AggregationOperation lookup = Aggregation.lookup(fromCollection, localField, toMatchWith, "arrayForeignObject");\n AggregationOperation unwind = Aggregation.unwind("arrayForeignObject", true);\nAggregationOperation replaceRoot = Aggregation.replaceRoot(Aggregation.ROOT);\nAggregationOperation project = Aggregation.project("arrayForeignObject").andExclude("_id");\nAggregationOperation out = Aggregation.out("aggregate");\n\nAggregation aggregation = Aggregation.newAggregation(lookup, replaceRoot, unwind, project, out);\n\nmongoTemplate.aggregate(aggregation, initialCollection, AggregateModel.class);\nRun Code Online (Sandbox Code Playgroud)\n\n我在以下几点上遇到了问题:\n {$replaceRoot: { newRoot: { $mergeObjects: [ "$arrayForeignObject", "$$ROOT" ] } …
我正在寻找一位翻译来改变这一点:
getCollection('migrate').aggregate([
{ "$project": {
"Contrat": {"Field1":"$Field1", "Field2":"$Field2"},
"Formule": {"Field3":"$Field3", "Field4":"$Field4"}
}},
{ "$project": {
"Contrats": {"Contrat":"$Contrat", "Formule":"$Formule"}
}}
])
Run Code Online (Sandbox Code Playgroud)
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);
Run Code Online (Sandbox Code Playgroud)
我在文档中找不到我的答案,我认为文档太差了,或者我可能太迷失在其中了(|愚蠢)。
我会提前感谢你的。