我有一个mongoDB聚合查询,使用它,我能够得到字段的长度,有最大的字符数在集合。我需要帮助将该聚合查询转换为等价的java.请找到下面的聚合查询。
db.getCollection('staff').aggregate([
{"$match": {"department": "technology"}},
{"$project": {"maxCharLength": {"$strLenCP": "$firstName"}}},
{"$sort": {"maxCharLength": -1}},{"$limit": 1}
])
I need to convert the above query to its equivalent in java.我需要将上述查询转换为等价的java代码。Please find the java code which im trying below: Im stuck with on how to use $strLenCP with project in java code below:
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);
Aggregation.project("firstName").andExpression("strLenCP(firstName)").as("length")