【问题标题】:How to project $strLenCP in Spring Data MongoDB [duplicate]如何在 Spring Data MongoDB 中投影 $strLenCP [重复]
【发布时间】: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);

【问题讨论】:

标签: java spring-boot mongodb-query aggregation-framework spring-data-mongodb


【解决方案1】:
    Aggregation.project("firstName").andExpression("strLenCP(firstName)").as("length")

【讨论】:

  • 请不要只发布代码作为答案,还要解释您的代码的作用以及它如何解决问题的问题。带有解释的答案通常更有帮助,质量更高,更有可能吸引投票。
猜你喜欢
  • 1970-01-01
  • 2021-03-22
  • 1970-01-01
  • 1970-01-01
  • 2020-07-01
  • 1970-01-01
  • 2018-03-05
  • 1970-01-01
  • 2016-02-23
相关资源
最近更新 更多