【发布时间】:2020-05-02 05:27:03
【问题描述】:
我有一个 mongoDB 聚合查询,使用它我能够获取集合中最大字符数的字段的长度。我需要帮助将该聚合查询转换为 java 中的等效查询。 请在下面找到汇总查询:
db.getCollection('staff').aggregate([
{"$match": {"department": "technology"}},
{"$project": {"maxCharLength": {"$strLenCP": "$firstName"}}},
{"$sort": {"maxCharLength": -1}},{"$limit": 1}
])
我需要将上述查询转换为 java 中的等效查询。请在下面找到我正在尝试的 java 代码:我坚持如何在下面的 java 代码中将 $strLenCP 与项目一起使用:
Aggregation agg = newAggregation(
match(Criteria.where("department").in("technology")),
project(""), //how to use $strLenCP here
sort(Sort.Direction.DESC, "maxCharLength"),
limit(1));
mongoTemplate.aggregate(agg, "staff", Staff.class);
【问题讨论】:
-
这能回答你的问题吗? Use $strLenCP with Spring Data MongoDB
标签: java spring-boot mongodb-query aggregation-framework spring-data-mongodb