如何将简单的mongo shell $匹配词组翻译为equivelent 在Java中的mongo-spring中 - 使用聚合?
$match: { $text: { $search: "read" } }
答案 0 :(得分:1)
Spring-data内置了对文本搜索的支持。
我使用了以下依赖项:
<dependency>
<groupId>org.springframework.data</groupId>
<artifactId>spring-data-mongodb</artifactId>
<version>1.8.2.RELEASE</version>
</dependency>
尝试以下语法:
TextCriteria criteria = TextCriteria.forDefaultLanguage().matchingAny("read");
Query query = TextQuery.queryText(criteria);
List<klass> list = mongoTemplate.find(query, klass, "collection_name");
有关详细信息,请参阅this。
要在聚合中使用以下语法:
BasicDBObject match = new BasicDBObject("$match",
new BasicDBObject("$text", new BasicDBObject("$search", "COST")));
List<DBObject> aggregationList = new ArrayList<DBObject>();
aggregationList.add(match);
AggregationOutput aggregationOutput = mongoTemplate.getCollection("categoryMaster")
.aggregate(aggregationList);
List<DBObject> dbObjects = (List<DBObject>) aggregationOutput.results();
在dbobjects
中转换此klass
,如下所示:
for(DBObject dbObject : dbObjects) {
mongoTemplate.getConverter().read(klass, dbObject);
}