4

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);

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

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

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

s7vr
  • 73,656
  • 11
  • 106
  • 127
Frigg0
  • 125
  • 3
  • 9

1 Answers1

5

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);

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);
s7vr
  • 73,656
  • 11
  • 106
  • 127
  • Since i have to use org.springframework.data spring-data-mongodb 2.1.0.RELEASE This dependency broke all my MongoTemplate autowiring. I don't get it why If I turn down this dependency i have no problems. Is that a compatibility issue or this dependency can't work along Mongo Java Driver 3.8.2 – Frigg0 Oct 02 '18 at 10:16
  • 2.1.0 [uses](https://github.com/spring-projects/spring-data-mongodb/commit/3cd95424836174d01fd0c0fa56991df4d5c76441) 3.8.2. So I'm not sure what exactly the problem that you are facing. I've added workaround for other versions. – s7vr Oct 02 '18 at 11:38
  • It's okay, i'm just stupid. If you use the springframework data 2.1 you also need the 2.1 spring core dependency. So i've been able to test what you propose. Work as intended Many thanks Veeram – Frigg0 Oct 02 '18 at 12:33
  • Hey Veeram, can you help me out on this one please? https://stackoverflow.com/questions/53213134/how-to-translate-mongodb-js-aggregation-pipeline-to-java-framework – Frigg0 Nov 09 '18 at 09:15