发布于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黑洞网
任何形式的转载都请注明出处,如有侵权 一经发现 必将追究其法律责任
昵称:
评论内容:(最多支持255个字符)
---无人问津也好,技不如人也罢,你都要试着安静下来,去做自己该做的事,而不是让内心的烦躁、焦虑,坏掉你本来就不多的热情和定力
Copyright © 2018-2021 java黑洞网 All Rights Reserved 版权所有,并保留所有权利。京ICP备18063182号-2
投诉与举报,广告合作请联系vgs_info@163.com或QQ3083709327
免责声明:网站文章均由用户上传,仅供读者学习交流使用,禁止用做商业用途。若文章涉及色情,反动,侵权等违法信息,请向我们举报,一经核实我们会立即删除!