聚合管道mongoDB到mongoTemplate.executeCommand(Java-SpringBoot)

时间:2018-11-13 16:52:38

标签: java mongodb aggregation mongo-java-driver

我想将此工作聚合管道与mongoTemplate.executeCommand一起使用,它以String jsoncommand或document命令作为参数,但是我找不到该死的语法。

管道:

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

例如,这在rob3t上有效。

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

这很好,但是我在超过2M的线路上进行了汇总。在执行聚合时,工作正常,但是Java尝试在???上发送其垃圾收集器。我真的不知道,也许它可以对不同的对象进行建模(我希望他不要,但是我不必选择)。导致GC错误。 在这些大数据量的情况下,我大肆宣传其他帖子是正常的。 我在mongoTemplate.aggregate(aggregation,initialCollection,model)中将'model.Class'归为一;负责此GC错误。

因此,我正在寻找一种将聚合管道直接发送到MongoDB的方法,以直接执行它而无需通过Java。

我正在做一个新问题,因为我尝试了其他人提出的许多解决方案,但是没有用,并且当我尝试在同一帖子中提问时,我被推后删除。 示例:mongotemplate aggregation with unique results 尝试过此操作,但是在传递给.aggregate()的args上,DBObject是无效的参数。

有人可以帮我吗? 干杯

编辑:

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

在这里打电话:

  mongoTemplate.executeCommand("{aggregate: 'collection', pipeline: "+query+"}");

0 个答案:

没有答案