如何在Spring Data MongoDB中投射$strLenCP [重复]。

问题描述 投票:0回答:2

我有一个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);
java spring-boot mongodb-query aggregation-framework spring-data-mongodb
2个回答
0
投票
    Aggregation.project("firstName").andExpression("strLenCP(firstName)").as("length")
© www.soinside.com 2019 - 2024. All rights reserved.