小编Fri*_*gg0的帖子

在 Spring MongoDB 中的 ReplaceRoot 管道阶段中使用 $mergeObjects

我希望将这个片段复制到 Java 代码中:

\n\n
db.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" }])\n
Run 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);\n
Run Code Online (Sandbox Code Playgroud)\n\n

我在以下几点上遇到了问题:\n {$replaceRoot: { newRoot: { $mergeObjects: [ "$arrayForeignObject", "$$ROOT" ] } …

java mongodb aggregation-framework spring-mongodb

4
推荐指数
1
解决办法
3654
查看次数

项目作为 spring mongo 中的嵌套文档

我正在寻找一位翻译来改变这一点:

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)

我在文档中找不到我的答案,我认为文档太差了,或者我可能太迷失在其中了(|愚蠢)。

我会提前感谢你的。

java mongodb aggregation-framework spring-data-mongodb

3
推荐指数
1
解决办法
2854
查看次数