java - 在 Spring MongoDB 的 ReplaceRoot 管道阶段使用 $mergeObjects
问题描述
我希望将此片段复制到 Java 代码中:
db.getCollection('admins_comptes_client_ceov4').aggregate([
{$lookup: {from: "contrats_ceov4",localField: "CUSTOMERNUMBER",foreignField: "CUSTOMERNUMBER",as: "arrayForeignObject"}
{$unwind: { path: "$arrayForeignObject", preserveNullAndEmptyArrays: true}},
{$replaceRoot: { newRoot: { $mergeObjects: [ "$arrayForeignObject", "$$ROOT" ] } }},
{ $project: { "arrayForeignObject": 0, "_id": 0 } },
{ $out: "aggregate" }])
到目前为止我在这里:
AggregationOperation lookup = Aggregation.lookup(fromCollection, localField, toMatchWith, "arrayForeignObject");
AggregationOperation unwind = Aggregation.unwind("arrayForeignObject", true);
AggregationOperation replaceRoot = Aggregation.replaceRoot(Aggregation.ROOT);
AggregationOperation project = Aggregation.project("arrayForeignObject").andExclude("_id");
AggregationOperation out = Aggregation.out("aggregate");
Aggregation aggregation = Aggregation.newAggregation(lookup, replaceRoot, unwind, project, out);
mongoTemplate.aggregate(aggregation, initialCollection, AggregateModel.class);
我在以下几点上有一个问题:
{$replaceRoot: { newRoot: { $mergeObjects: [ "$arrayForeignObject", "$$ROOT" ] } }
我无法成功创建一个 mergeObjects。使用以下 java 代码段,AggregationOperation 结果为:
"$replaceRoot" : { "newRoot" : "$arrayForeignObject" }
当我执行这个 java 代码片段时,我最终得到的新集合只有内部的外部数组和一个 _id 字段。
有没有人已经面临这个问题,可以帮忙吗?弗里格0
解决方案
你在这里有几个问题。使用 2.1.0 发布 Spring Mongodb jar。
AggregationOperation replaceRoot = Aggregation.replaceRoot().withValueOf(ObjectOperators.valueOf("arrayForeignObject").mergeWith(Aggregation.ROOT));
AggregationOperation project = Aggregation.project().andExclude("_id", "arrayForeignObject");
Aggregation aggregation = Aggregation.newAggregation(lookup, unwind, replaceRoot, project, out);
对于较低版本的spring mongodb
AggregationOperation replaceRoot = ReplaceRootOperation.builder().withDocument("$mergeObjects", Arrays.asList("$arrayForeignObject", Aggregation.ROOT));
AggregationOperation project = Aggregation.project().andExclude("_id", "arrayForeignObject");
Aggregation aggregation = Aggregation.newAggregation(lookup, unwind, replaceRoot, project, out);
推荐阅读
- python - Django,如何对字典值中的查询集值求和
- r - 在R中的矩阵中随机获取固定数量的零
- javascript - 无法使用更高版本的包将数组转换为列表
- python - 如何使用 CSS 显示具有相同高度的图像?
- ios - iOS - 新的 Firebase Crashlytics (4.0.0-beta.5) - dSYM 上传失败
- opentok - opentok SDK 和 opentok REST API 有什么区别?
- python - 如何改进这个递归函数?
- tensorflow - CNN 模型的输出为 1.0 或 0.0
- javascript - 错误错误:找不到带有路径的控件:'responses -> response'(Angular 8)FormArray
- cassandra - Cassandra Where 子句中的 IN 查询