[英]How to write a MongoDB aggregate query in java
我不知道如何用Java編寫此查詢
db.wordsRank.aggregate ([
{$match: {word: {$regex:/engin/i} } },
{$sort:{rank:-1} }
]
)
我試圖將其分成文檔,然后像這樣聚合它們:
Document regQuery = new Document();
regQuery.append("$regex", "/" + "engin" + "/i");
Document wordQuery = new Document();
wordQuery.append("word", regQuery);
Document matchQuery = new Document();
matchQuery.append("$match", wordQuery);
Document rankQuery = new Document();
rankQuery.append("rank", -1);
Document sortQuery = new Document();
sortQuery.append("$sort", rankQuery);
Iterator iterDoc2=collection.aggregate(
Arrays.asList(
matchQuery,
sortQuery
)
).iterator();
但是它返回null迭代器,我該怎么做才能解決此問題?
只是我將$ regex的查詢更改為:
Document regQuery = new Document();
regQuery.append("$regex", "^(?i)" + Pattern.quote("engin"));
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.