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+"}");