英文:
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")
专注分享java语言的经验与见解,让所有开发者获益!
评论