confirmed_at_to_dates.mjs 1.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354
  1. import { db, waitForDb } from '../app/src/infrastructure/mongodb.js'
  2. import { fileURLToPath } from 'url'
  3. async function updateStringDates() {
  4. await waitForDb()
  5. const users = await db.users.aggregate([
  6. { $unwind: { path: '$emails' } },
  7. {
  8. $match: { 'emails.confirmedAt': { $exists: true, $type: 'string' } },
  9. },
  10. {
  11. $project: {
  12. _id: 1,
  13. 'emails.email': 1,
  14. 'emails.confirmedAt': 1,
  15. },
  16. },
  17. ])
  18. let user
  19. let count = 0
  20. while ((user = await users.next())) {
  21. count += 1
  22. if (count % 10000 === 0) {
  23. console.log(`processed ${count} users`)
  24. }
  25. const confirmedAt = user.emails.confirmedAt
  26. const dateConfirmedAt = new Date(confirmedAt.replace(/ UTC$/, ''))
  27. await db.users.updateOne(
  28. {
  29. _id: user._id,
  30. 'emails.email': user.emails.email,
  31. },
  32. {
  33. $set: {
  34. 'emails.$.confirmedAt': dateConfirmedAt,
  35. },
  36. }
  37. )
  38. }
  39. console.log(`Updated ${count} confirmedAt strings to dates!`)
  40. }
  41. if (fileURLToPath(import.meta.url) === process.argv[1]) {
  42. try {
  43. await updateStringDates()
  44. process.exit(0)
  45. } catch (error) {
  46. console.error(error)
  47. process.exit(1)
  48. }
  49. }
  50. export default updateStringDates