1

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" } } 
4

1 に答える 1

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");

詳細については、これを参照してください。

集計で同じことを行うには、次の構文を使用します。

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);
}
于 2016-01-21T10:03:27.710 に答える