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

298
Views
La agregación de MongoTemplate replaceRoot no funciona

Estoy trabajando con SpringBoot + Mongodb usando Mongotemplate.

Tengo una colección de paquetes con el siguiente documento de muestra:

 { "_id" : ObjectId("61de8228a992b10804b3f1ae"), "pack_uuid" : "f67cb514-326c-4933-8e23-0580c912896c", "pack_name" : "covid 19", "pack_description" : "covid is dengeours", "created_by" : "ea41c6b8-aaec-4d9b-a5a7-a6a72ca4c9f6", "members" : [{ "_id" : "0aa4b098-aab3-4ccf-8401-d52e6a4e42cc", "user_uuid" : "1f9b5f0b-f9f6-45d7-b25b-a5319307569f", "joining_date" : ISODate("2022-01-12T16:24:34.719Z") },{ "_id" : "0aa4b098-aab3-4ccf-8401-d52e6a4e42cc", "user_uuid" : "2f9b5f0b-f9f6-45d7-b25b-a5319307569f", "joining_date" : ISODate("2022-01-12T16:24:34.719Z") }] }

Necesito implementar la condición where que verifica si el usuario pasado es miembro del paquete o no. Algo como:

 "members.user_uuid": "1f9b5f0b-f9f6-45d7-b25b-a5319307569f"

y devolver solo los siguientes campos:

 "pack_uuid" : "f67cb514-326c-4933-8e23-0580c912896c", "pack_name" : "covid 19", "pack_description" : "covid is dengeours", "created_by" : "ea41c6b8-aaec-4d9b-a5a7-a6a72ca4c9f6",

Probé el siguiente código:

 AggregationOperation match = Aggregation.match( Criteria.where("members.user_uuid").is(userUUID) ); AggregationOperation unwind = Aggregation.unwind("members"); AggregationOperation group = Aggregation.group("_id"); AggregationOperation replaceRoot = Aggregation.replaceRoot(Aggregation.ROOT); List<AggregationOperation> operations = new ArrayList<>(); operations.add(unwind); operations.add(match); operations.add(group); operations.add(replaceRoot); Aggregation aggregation = Aggregation.newAggregation(operations); List<Pack> packs = mongoTemplate.aggregate(aggregation, Pack.class, Pack.class).getMappedResults();

Estoy recibiendo este error:

 Invalid reference '$$ROOT'

¿Qué estoy haciendo mal?

over 4 years ago · Santiago Trujillo
1 answers
Answer question

0

No necesita replaceRoot en este caso ya que los campos que desea proyectar ya están en la raíz. Además, no se necesitan canalizaciones $unwind y $group , ya que $match es suficiente para devolver documentos coincidentes para la consulta dada.

En su lugar, excluya el campo de miembros mediante ProjectionOperation de la siguiente manera:

 AggregationOperation match = Aggregation.match( Criteria.where("members.user_uuid").is(userUUID) ); ProjectionOperation project = Aggregation.project().andExclude("members"); List<AggregationOperation> operations = new ArrayList<>(); operations.add(match); operations.add(project); Aggregation aggregation = Aggregation.newAggregation(operations); List<Pack> packs = mongoTemplate.aggregate(aggregation, Pack.class, Pack.class).getMappedResults();
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!