split_tests_assigned_at_to_dates.mjs 1.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546
  1. import { db } from '../app/src/infrastructure/mongodb.js'
  2. import { fileURLToPath } from 'node:url'
  3. async function updateStringDates() {
  4. const users = db.users.find({
  5. splitTests: { $exists: true },
  6. })
  7. let user
  8. let count = 0
  9. while ((user = await users.next())) {
  10. count += 1
  11. if (count % 10000 === 0) {
  12. console.log(`processed ${count} users...`)
  13. }
  14. const splitTests = user.splitTests
  15. for (const splitTestKey of Object.keys(splitTests)) {
  16. for (const variantIndex in splitTests[splitTestKey]) {
  17. splitTests[splitTestKey][variantIndex].assignedAt = new Date(
  18. splitTests[splitTestKey][variantIndex].assignedAt
  19. )
  20. }
  21. }
  22. await db.users.updateOne(
  23. {
  24. _id: user._id,
  25. },
  26. { $set: { splitTests } }
  27. )
  28. }
  29. console.log(`Updated ${count} assignedAt strings to dates!`)
  30. }
  31. if (fileURLToPath(import.meta.url) === process.argv[1]) {
  32. try {
  33. await updateStringDates()
  34. process.exit(0)
  35. } catch (error) {
  36. console.error(error)
  37. process.exit(1)
  38. }
  39. }
  40. export default updateStringDates