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

178
Visualizações
Filter MongoDB Collections with a multiply variations

I have a collection like this:

[
    {"userId": "0000", "algorithm": "algo1", "status": "Running",   "waitingTime": 0},
    {"userId": "0001", "algorithm": "algo1", "status": "Received",  "waitingTime": 0},
    {"userId": "0000", "algorithm": "algo2", "status": "Completed", "waitingTime": 123},
    {"userId": "0000", "algorithm": "algo2", "status": "Error",     "waitingTime": 134},
    {"userId": "0001", "algorithm": "algo2", "status": "Error",     "waitingTime": 150},
    {"userId": "0001", "algorithm": "algo3", "status": "Completed", "waitingTime": 100},
    {"userId": "0000", "algorithm": "algo3", "status": "Completed", "waitingTime": 120},
    {"userId": "0001", "algorithm": "algo1", "status": "Received",  "waitingTime": 0}
]

I need to find each user's maximum waiting time but only with status "Completed". The output should have 2 props for each document.

  • "_id" - which should be "userId" from original collection
  • "maxWaitingTime" - should be equal to the maximum waiting time for status "Completed"

For this example the output should be something like this:

[
    {"_id": "0000", "maxWaitingTime": 123},
    {"_id": "0001", "maxWaitingTime": 100}
]

I tried to combine the some filtering queries together but it didn't worked out well. I can combine one or two statements but for this one I'm strugle.

I tried to combine the distinct method with find to get unique userId's but it didn't worked:

db.tasks.distinct("userId").find();

Also I tried to do the logic inside of the find fucntion:

printjsononeline(db.tasks.distinct(userId).find({
    userId: "0001"
}).map(doc => doc));

But unfortunetly it doesn't worked eather.

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

0

You'll need to use an aggregation (see https://docs.mongodb.com/manual/aggregation/ ). The match operator will allow you to filter on the status, and the group operator will allow you to get the maximum value for the wait time for a given user ID.

over 4 years ago · Santiago Trujillo Relatório

0

Playground

db.collection.aggregate([
  {
    "$match": {//Filter completed
      "status": "Completed"
    }
  },
  {
    $group: {//Group by user id and find max
      "_id": "$userId",
      "waitingTime": {
        "$max": "$waitingTime"
      }
    }
  }
])

Note: Mongo is case sensitive. And people do use waiting_time than waitingTime.

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