Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

452
Views
Usar $strLenCP con Spring Data MongoDB

tengo esta consulta mongodb

 db.getCollection('myCollection').aggregate( [{ $project: { length: { $strLenCP: "$prefix" } } }, { $sort: { length: -1 } }] )

que quiero usar en un proyecto java de primavera pero no puedo escribir el código java correcto (el tipo no es el problema).

probé esto

 Aggregation agg = newAggregation(project().andExpression("strLenCP(prefix)").as("prefixLength")); AggregationResults < RequestSettingsWithPrefixLength > results = mongoTemplate.aggregate(agg, RequestSettings.class, RequestSettingsWithPrefixLength.class); List < RequestSettingsWithPrefixLength > requestSettingsList = results.getMappedResults();

pero obtengo una clave nula en agg JSON (modo de depuración):

 { "aggregate": "__collection__", "pipeline": [{ { "$project": { "prefixLength": { "null": ["$prefix"] } } }] } }

Puedo ver que mi objeto agregado tiene esta operación de proyección:

 expression -> strLenCP(prefix) field -> AggregationField: AggregationField - name: prefixLength, target: prefixLength, synthetic: true params -> []

No estoy seguro de que esto sea correcto, pero no puedo encontrar ninguna documentación que use strLenCP. Solo encontré esta prueba que usa la proyección strLenCP: https://github.com/spring-projects/spring-data-mongodb/blob/dc57b66adfd60b4d69d1d349b4fcfa4ab0da95e7/spring-data-mongodb/src/test/java/org/springframework/data /mongodb/core/aggregation/SpelExpressionTransformerUnitTests.java#L922

¿Alguien podría ayudar por favor?

Salud

over 4 years ago · Santiago Trujillo
3 answers
Answer question

0

El soporte para los operadores de agregación Mongo3.4 se agregó en 1.10.0.RC1. Si está de acuerdo con la actualización para lanzar la versión candidata, todo debería funcionar bien.

O puede intentar lo siguiente, pero necesitará usar la versión 1.8.5.

 Aggregation aggregation = newAggregation( project().and(new AggregationExpression() { @Override public DBObject toDbObject(AggregationOperationContext aggregationOperationContext) { return new BasicDBObject("$strLenCP", "$prefix"); } }).as("prefixLength") );
over 4 years ago · Santiago Trujillo Report

0

Puede usar la expresión project(...).and(valueOf("fieldName").lengthCP()).as("fieldLength")

over 4 years ago · Santiago Trujillo Report

0

La respuesta de @kairius no está clara. Así es como debe hacerse: Use StringOperators#valueOf seguido de lengthCP() :

 TypedAggregation.project() .and(StringOperators.valueOf("prefix").lengthCP()) .as("length")
over 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!