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

170
Views
grupo agregado del controlador java mongodb por

Estoy tratando de escribir un grupo por función agregada usando el controlador java mongodb.

Aquí está la estructura del documento de la base de datos.

 { "_id" : ObjectId("58819bd9f16a7802523bc077"), "Date" : "12/19/2016", "Time" : "4:15:00", "Temperature" : 65.5, "User" : "A", "ThermalComfort" : -1, "settingID" : ObjectId("58819bd6f16a7802523bbdc5") } { "_id" : ObjectId("58819bd9f16a7802523bc078"), "Date" : "12/19/2016", "Time" : "4:30:00", "Temperature" : 65.25, "User" : "A", "ThermalComfort" : -1, "settingID" : ObjectId("58819bd6f16a7802523bbdc5") }

Me gustaría agrupar por "Fecha" y "Hora", proyectando solo ThermalComfort. Mi resultado esperado es este: {fecha = 19/12/16, hora = 0:00:00, listOfTC = [2,1]}. listOfTC es una lista hecha de cada lectura en Array: (por ejemplo, [-1,-1])

Aquí está el código que escribí.

 AggregateIterable<Document> iterable = themalComfortProfileCollection.aggregate(Arrays.asList( new Document("$group", new Document("_id", "$Date" + "$Time").append("count", new Document("$sum", 1))), new Document("$project", new Document("comfortIndex", "$ThermalComfort"))));

Sin embargo, si imprimo el documento, obtengo un documento nulo.

 for (Document doc : iterable) { System.out.println(doc); } Document{{_id=null}}

¿Podrías explicar qué parte está mal?

over 4 years ago · Santiago Trujillo
1 answers
Answer question

0

Entonces, finalmente, este código devolvió el resultado que quería.

 AggregateIterable<Document> iterable = themalComfortProfileCollection.aggregate(Arrays.asList( new Document("$group", new Document("_id", new Document("date", "$Date").append("time", "$Time") ).append("ThermalComfortList", new Document("$push", "$ThermalComfort"))), new Document("$sort", new Document("_id", 1))));
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!