How do I translate a simple mongo shell $match phrase, to it's equivelent in mongo-spring in Java - using aggregation?
$match: { $text: { $search: "read" } }
Spring-data has inbuilt support for text search.
I have used following dependency :
<dependency>
<groupId>org.springframework.data</groupId>
<artifactId>spring-data-mongodb</artifactId>
<version>1.8.2.RELEASE</version>
</dependency>
Try Following Syntax :
TextCriteria criteria = TextCriteria.forDefaultLanguage().matchingAny("read");
Query query = TextQuery.queryText(criteria);
List<klass> list = mongoTemplate.find(query, klass, "collection_name");
For more detail refer this.
To do the same in aggregation use following syntax :
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();
convert this dbobjects
in your klass
as below :
for(DBObject dbObject : dbObjects) {
mongoTemplate.getConverter().read(klass, dbObject);
}