Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

169
Visualizações
mongodb java driver aggregate group by

I am trying to write group by aggregate function using mongodb java driver.

Here's the document structure of database.

{ "_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") }

I would like to group by "Date" and "Time", projecting ThermalComfort only. My expected output is this: {date=12/19/16, time=0:00:00, listOfTC = [2,1]}. listOfTC is a list made of each readings in Array: (e.g. [-1,-1])

Here's the code that I wrote.

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

However, if I print out document, I am getting null document.

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

Document{{_id=null}}

Could you explain which part is wrong?

over 4 years ago · Santiago Trujillo
1 Respostas
Responde à pergunta

0

So, finally, this code returned a result what I wanted.

    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 Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda