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

214
Views
MongoDB: busque por campo con fecha y actualícelo por condición

Dígame cómo puedo cumplir la siguiente condición: si la hora en el campo info.startDate no es igual a 00 horas, aumente la fecha (2021-05-27) 1 día antes, configure la hora a las 00:00: 00.000Z. Traté de hacerlo torpemente, a través de Mongock, obteniendo todos los elementos de la colección y haciendo una verificación a través de LocalDateTime, pero mi montón se desbordó, lo cual es lógico porque la colección es grande. ¿Cómo puedo hacer esto a través de Mongock o al menos una solicitud manual a MongoDB? Hasta ahora solo he escrito esto:

 db.getSiblingDB("ervk_core").getCollection("supervision").updateMany( {}, [ { "$set": { "info.startDate": { "$cond": { if: { $eq: [ "$info.startDate", (there should be a condition at midnight) ] }, then: (here the day should be added to full and the time should be set to midnight) } } } } ])

Me gustaría usar dateToString para hacer una búsqueda parcial por hora, pero según tengo entendido, esta función solo se puede usar en una agregación.

te agradeceria tu ayuda :)

over 4 years ago · Santiago Trujillo
1 answers
Answer question

0

Si está utilizando la versión 5.0+ de Mongo, puede usar $dateTrunc y $dateAdd para lograr esto con bastante facilidad, así:

 db.collection.updateMany( {}, [ { $set: { "info.startDate": { $cond: [ { $ne: [ { $hour: "$info.startDate" }, 0 ] }, { $dateTrunc: { date: { $dateAdd: { startDate: "$info.startDate", unit: "day", amount: 1 } }, unit: "day", } }, "$info.startDate" ] } } } ])

Para versiones anteriores de Mongo, esto es un poco más complicado, debe usar $dateFromParts para crear el nuevo objeto de fecha, así:

 db.collection.updateMany( {}, [ { $set: { "info.startDate": { $cond: [ { $ne: [ { $hour: "$info.startDate" }, 0 ] }, { $dateFromParts: { "year": { $year: { $add: [ "$info.startDate", 86400000 ] } }, "month": { $month: { $add: [ "$info.startDate", 86400000 ] } }, "day": { $dayOfMonth: { $add: [ "$info.startDate", 86400000 ] } }, "hour": 0, "minute": 0, "second": 0, "millisecond": 0, } }, "$info.startDate" ] } } } ])

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!