remove_brand_variation_ids.js 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  1. //
  2. // Remove the brandVariationId attribute from project documents that have
  3. // that attribute, which value matches the one given.
  4. //
  5. // node scripts/remove_brand_variation_ids.js 3
  6. // gives a report of project documents that have brandVariationId attribute
  7. // with value, "3"
  8. //
  9. // node scripts/remove_brand_variation_ids.js 3 --commit true
  10. // actually removes the brandVariationId attribute from project documents
  11. // that have brandVariationId attribute with value, "3"
  12. //
  13. const { db } = require('../app/js/infrastructure/mongojs')
  14. const async = require('async')
  15. const minimist = require('minimist')
  16. const argv = minimist(process.argv.slice(2))
  17. const bvId = argv._[0]
  18. const commit = argv.commit !== undefined
  19. console.log(
  20. (commit ? 'Remove' : 'Dry run for remove') +
  21. ' brandVariationId from projects that have { brandVariationId: ' +
  22. bvId +
  23. ' }'
  24. )
  25. var count = 0
  26. db.projects.find(
  27. { brandVariationId: bvId.toString() },
  28. { _id: 1, name: 1 },
  29. processRemovals
  30. )
  31. function processRemovals(err, projects) {
  32. if (err) throw err
  33. async.mapSeries(
  34. projects,
  35. function(project, cb) {
  36. count += 1
  37. console.log(
  38. (commit ? 'Removing' : 'Would remove') +
  39. ' brandVariationId on project ' +
  40. project._id +
  41. ', name: "' +
  42. project.name +
  43. '"'
  44. )
  45. if (commit) {
  46. db.projects.update(
  47. { _id: project._id },
  48. { $unset: { brandVariationId: '' } },
  49. cb
  50. )
  51. } else {
  52. cb()
  53. }
  54. },
  55. function(err) {
  56. if (err) {
  57. console.log('There was a problem: ', err)
  58. }
  59. console.log(
  60. 'BrandVariationId ' +
  61. (commit ? 'removed' : 'would be removed') +
  62. ' from ' +
  63. count +
  64. ' projects'
  65. )
  66. process.exit()
  67. }
  68. )
  69. }