4

I'd like to use this working aggregation pipeline with mongoTemplate.executeCommand which takes as argument a String jsoncommand or a document command, but i can't find the damn syntax.

pipeline :

> db.getCollection('collectionName').aggregate([
{   
   $lookup: {  
       from: "SecondCollectionName",   
       localField: "field",    
       foreignField: "toMatch",    
       as: "arrayForeignObject"   
           }
},  
{ $unwind: { path: "$arrayForeignObject", preserveNullAndEmptyArrays: true}},    
{
    $replaceRoot: { newRoot: { $mergeObjects: [ "$arrayForeignObject", "$$ROOT" ] } }
},   
{ $project: { "arrayForeignObject": 0, "_id": 0, "IDDIRECTORYITEM":0 } },   
{ $out: "collectionName" }
])

This works on rob3t for exemple.

using this in java:

AggregationOperation lookup = Aggregation.lookup(fromCollection, localField, toMatchWith, arrayForeignObject);
AggregationOperation project = Aggregation.project().andExclude(arrayForeignObject, "_id", argsString);
AggregationOperation unwind = Aggregation.unwind(arrayForeignObject, true);
AggregationOperation replaceRoot = Aggregation.replaceRoot().withValueOf(ObjectOperators.valueOf(arrayForeignObject).mergeWith(Aggregation.ROOT));
AggregationOperation out = Aggregation.out(finalCollection);
Aggregation aggregation = Aggregation.newAggregation(lookup, unwind, replaceRoot, project, out);

mongoTemplate.aggregate(aggregation, initialCollection, model);

This works fine, but i do an aggregation on more than 2M lines. On execution aggregation works, but java trying to send his Garbage Collector on ??? i don't really know, maybe it modelize the different objects ( i'd prefer he don't but i don't have to choose). Resulting a GC error. I rade on other posts that is normal with these large data size. I incriminate the 'model.Class' in mongoTemplate.aggregate(aggregation, initialCollection, model); to be responsible of this GC error.

So i'm looking for a way to send the aggregation pipeline directly to MongoDB to execute it directly without passing through Java.

I'm doing a new question, beccause i try many solutions proposed by others but none works, and when i tried to ask on the same post i was push back and delete. example: mongotemplate aggregation with unique results tried this, but on the args passed to .aggregate(), DBObject are invalid arguments.

Can someone help me out, please? Cheers

EDIT :

 String query = "{$lookup: {" +    
                "       from: \"collection2\"," +     
                "       localField: \"VISIBLEONLINE\"," +     
                "       foreignField: \"IDDIRECTORYITEM\"," +     
                "       as: \"arrayForeignObject\" }}," +     
                "{ $unwind: { path: \"$arrayForeignObject\", preserveNullAndEmptyArrays: true}}," +     
                "{ $replaceRoot: { newRoot: { $mergeObjects: [ \"$arrayForeignObject\", \"$$ROOT\" ] } }}," +      
                "{ $project: { \"arrayForeignObject\": 0, \"_id\": 0, \"IDDIRECTORYITEM\":0 } }," +      
                "{ $out: \"collection\" }";      

Here the call :

  mongoTemplate.executeCommand("{aggregate: 'collection', pipeline: "+query+"}");
Frigg0
  • 125
  • 3
  • 9
  • 1
    Something like `mongoTemplate.executeCommand({aggregate:'collectionName',pipeline:your aggregation query})` – s7vr Nov 13 '18 at 18:19
  • I tried > mongoTemplate.executeCommand("{aggregate: 'collection', pipeline: "+query+"}"); Result : JSON reader was expecting a name but found '{'. > mongoTemplate.executeCommand(aggregate: 'collection', pipeline: "+query); JSON reader was expecting a value but found 'aggregate'. > mongoTemplate.executeCommand({aggregate: 'collection', pipeline: "+query}); This doesn't compile – Frigg0 Nov 14 '18 at 08:59
  • And before that i tried many other syntaxes, like conserver a JS like command as for Rob3t. No success. I wonder if it's still achievable since the different versions of the java driver. Documentation give you the name of the different command and that's all. – Frigg0 Nov 14 '18 at 09:04
  • 1
    Can you show me what `query` variable has ? – s7vr Nov 14 '18 at 13:42
  • Sure, i added this on the question preceded by EDIT for more visibility Doc says : it takes a String jsoncommand as argument I don't know for sure what look like a jsoncommand as a String. – Frigg0 Nov 14 '18 at 13:57
  • Try `String query = "[{$lookup....\"collection\" }]";` – s7vr Nov 14 '18 at 13:59
  • new error : ERROR - org.bson.json.JsonParseException: Invalid JSON number – Frigg0 Nov 14 '18 at 14:35
  • I didnt get any error. Is there more to your query ? – s7vr Nov 14 '18 at 14:37
  • the number in JSON can be 6 for the integer value or "6" for a parameter. In this case 0 isn't a value more like a false as a String. I correct it. I'm struggling with the perfect syntax now. i have a Invalid JSON input. Position: 489. Character: ! !. seems to be a white space or invisible character. – Frigg0 Nov 14 '18 at 14:41
  • Let us [continue this discussion in chat](https://chat.stackoverflow.com/rooms/183640/discussion-between-frigg0-and-veeram). – Frigg0 Nov 14 '18 at 14:50

0 Answers0