這里有一個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查詢陳述句。
uj5u.com熱心網友回復:
已經解決,能成功通過介面訪問獲得結果仿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);
}
uj5u.com熱心網友回復:
https://blog.csdn.net/laoxiong_8080/article/details/84838858轉載請註明出處,本文鏈接:https://www.uj5u.com/shujuku/76701.html
標籤:MongoDB
