如何在Spring Data MongoDB中映射 `$strLenCP`。

huangapple 未分类评论49阅读模式
英文:

How to project $strLenCP in Spring Data MongoDB

问题

Sure, here's the translated version of your provided content:

Aggregation agg = newAggregation(
    match(Criteria.where("department").is("technology")),
    project().andExpression("strLenCP(firstName)").as("maxCharLength"),
    sort(Sort.Direction.DESC, "maxCharLength"),
    limit(1)
);
mongoTemplate.aggregate(agg, "staff", Staff.class);

Please note that in the Java code, I've used the andExpression method to incorporate the $strLenCP function within the $project stage of the aggregation pipeline. This should achieve the equivalent functionality of your MongoDB aggregate query.

英文:

I have a mongoDB aggregate query using which i was able to get the length of the field which has max no of characters in the collection. I need help to convert that aggregate query to its equivalent in java.
Please find the aggregate query below:

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

答案1

得分: 0

Aggregation.project("firstName").andExpression("strLenCP(firstName)").as("length")

英文:
    Aggregation.project("firstName").andExpression("strLenCP(firstName)").as("length")

huangapple
  • 本文由 发表于 2020年5月2日 13:27:03
  • 转载请务必保留本文链接:https://java.coder-hub.com/61554893.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定