在Spring MongoDB中的ReplaceRoot管道阶段内使用$ mergeObjects [英] Use $mergeObjects inside ReplaceRoot pipeline stage in Spring MongoDB

查看:654
本文介绍了在Spring MongoDB中的ReplaceRoot管道阶段内使用$ mergeObjects的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我希望将此片段重现为Java代码:

i'm looking to reproduce this snipet into Java code :

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" }])

到目前为止,我在这里:

I'm here so far :

 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结果为:

I've got an issue on the following point : {$replaceRoot: { newRoot: { $mergeObjects: [ "$arrayForeignObject", "$$ROOT" ] } } I can't succeed to make a mergeObjects. With this following java snippet, the AggregationOperation outcome is :

"$replaceRoot" : { "newRoot" : "$arrayForeignObject" } 

当我执行此Java代码段时,最终得到的新集合只有内部的外部数组和_id字段.

When i execute this java snippet, i end up with the new collection having only the foreign array inside and an _id field.

有人有没有遇到过这种情况,可以帮忙吗? Frigg0

Does anyone faced this already and could give a hand please? Frigg0

推荐答案

您在这里遇到了几个问题.使用2.1.0发行版Spring Mongodb jar.

You have couple of issues here. Use 2.1.0 Release 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的较低版本

For lower versions of 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);

这篇关于在Spring MongoDB中的ReplaceRoot管道阶段内使用$ mergeObjects的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆