confirmed_at_to_dates.mjs 1.2 KB

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