将原始mongodb聚合查询语句转成spring date mongodb查询语句

Goo丶 2018-12-05 09:46:41
这里有一个mongo的原生语句,功能是聚合我自定义的list与原数据中的一个list的交集
db.getCollection('hb_question_standards2').aggregate([
{ $project: { title: 1,commonToBoth: { $setIntersection: [ ["缴纳","车船税","机动","车船","时间","动车","机动车","缴纳时间","规定","如何","如何缴纳"], "$mapDarkTitle.word" ] } }},
{ $project: { title: 1,commonToBoth:1,commonToBothSize: { $size: [ "$commonToBoth"] } }},
{$sort: {"commonToBothSize": -1}},
{$limit: 30},
]);
其中["缴纳","车船税","机动","车船","时间","动车","机动车","缴纳时间","规定","如何","如何缴纳"]是可变的,执行上面语句得到如下结果。(是预期结果)

现在想把词语句用spring date mongodb写出来,查阅资料后尝试无果。
本人学生党,理解不够深,希望有前辈花几分钟时间指点一番,我这个原生语句怎么转成对应spring date mongodb查询语句。
...全文
185 2 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
2 条回复
切换为时间正序
请发表友善的回复…
发表回复
Goo丶 2018-12-05
  • 打赏
  • 举报
回复
https://blog.csdn.net/laoxiong_8080/article/details/84838858
Goo丶 2018-12-05
  • 打赏
  • 举报
回复
已经解决,能成功通过接口访问获得结果
仿monggo原始语句,测试版,还没有规范代码,相信能够看懂。

List<Document> dbObjects = new ArrayList<>();

Document limit = new Document();
Document project = new Document();
Document condition = new Document();
Document project2 = new Document();
Document condition2 = new Document();
Document sort = new Document();

condition.put("title", 1);
List<Object> listIntersects = new ArrayList<>();
listIntersects.add(words);
listIntersects.add("$mapDarkTitle.word");
condition.put("commonToBoth", new Document("$setIntersection", listIntersects));
project.put("$project", condition);

condition2.put("title", 1);
condition2.put("commonToBoth", 1);
condition2.put("commonToBothSize", new Document("$size", "$commonToBoth"));
project2.put("$project", condition2);

sort.put("$sort", new Document("commonToBothSize", -1));
limit.put("$limit", 20);

dbObjects.add(project);
dbObjects.add(project2);
dbObjects.add(sort);
dbObjects.add(limit);

AggregateIterable<Document> re = mongoTemplate.getCollection("hb_question_standards2")
.aggregate(dbObjects);

List<Document> listDocument = new ArrayList<>();
for (Iterator<Document> it = re.iterator(); it.hasNext();) {
Document dbo = it.next();
listDocument.add(dbo);
}

1,798

社区成员

发帖
与我相关
我的任务
社区描述
MongoDB相关内容讨论区
社区管理员
  • MongoDB社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧