程序员最近都爱上了这个网站  程序员们快来瞅瞅吧!  it98k网:it98k.com

本站消息

站长简介/公众号

  出租广告位,需要合作请联系站长


+关注
已关注

分类  

暂无分类

标签  

暂无标签

日期归档  

暂无数据

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

发布于2021-08-03 08:33     阅读(1074)     评论(0)     点赞(12)     收藏(5)


我想将这个工作聚合管道与 mongoTemplate.executeCommand 一起使用,它以字符串 jsoncommand 或文档命令为参数,但我找不到该死的语法。

管道:

> 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); 对这个 GC 错误负责。

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

我正在做一个新问题,因为我尝试了其他人提出的许多解决方案,但都没有奏效,当我尝试在同一个帖子上提问时,我被推回并删除。示例:具有唯一结果的 mongotemplate 聚合 尝试过此操作,但在传递给 .aggregate() 的参数上,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+"}");

解决方案


暂无回答



所属网站分类: 技术文章 > 问答

作者:黑洞官方问答小能手

链接:http://www.javaheidong.com/blog/article/254443/1108a4bd3a35c62fcfbc/

来源:java黑洞网

任何形式的转载都请注明出处,如有侵权 一经发现 必将追究其法律责任

12 0
收藏该文
已收藏

评论内容:(最多支持255个字符)