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

435
Views
Intervalo de tiempo medio en horas, minutos, segundos Mongodb

Estoy tratando de obtener el intervalo de tiempo promedio para todos los documentos de una colección.

La salida que estoy tratando de averiguar está en este formato Promedio: HH: MM: SS

Calculará el intervalo de tiempo para cada documento y luego lo agregará para obtener el intervalo de tiempo promedio para todo el conjunto de datos.

Estos son datos de muestra.

 { "_id" : ObjectId("60dc1e7d72296329347b2bbe"), "name": "firstupdate", "starttime" : ISODate("2021-06-30T07:38:06.926Z"), "endtime" : ISODate("2021-06-30T12:35:08.265Z"), }, { "_id" : ObjectId("60dc1e7d72296329347b2bce"), "name": "secondupdate", "starttime" : ISODate("2021-07-29T07:41:06.926Z"), "endtime" : ISODate("2021-07-30T01:52:07.937Z"), }, { "_id" : ObjectId("60dc1ff472d9f809d6d2f23e"), "name": "thirdupdate", "starttime" : ISODate("2021-07-15T07:43:06.926Z"), "endtime" : ISODate("2021-07-14T10:34:13.269Z"), }, { "_id" : ObjectId("60dc204e03362e293a5f5014"), "name": "fourthupdate", "starttime" : ISODate("2021-07-21T05:11:23.654Z"), "endtime" : ISODate("2021-07-21T09:46:33.000Z"), }, { "_id" : ObjectId("60dc21436a9e0e09f9a551ae"), "name": "fifthupdate", "starttime" : ISODate("2021-07-07T02:34:06.926Z"), "endtime" : ISODate("2021-07-07T08:11:06.926Z"), },

Gracias de antemano

over 4 years ago · Santiago Trujillo
1 answers
Answer question

0

Puedes usar este:

 db.collection.aggregate([ { $group: { _id: null, diff: { $avg: { $dateDiff: { startDate: "$starttime", endDate: "$endtime", unit: "millisecond" } } } } }, { $set: { diff: { $dateToString: { date: { $toDate: "$diff" }, format: "%H:%M:%S" } } } } ])

Opcionalmente, utilice el format: "%j %H:%M:%S" , %j es el día del año, es decir, esto devolvería una salida válida hasta un año. De lo contrario, necesitaría algunas matemáticas de fecha.

Parque infantil Mongo

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!