init-art-open.js 1.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243
  1. const models = require('../models');
  2. const firstOpenCount = 2000; // 初始开放2000个内容
  3. async function init() {
  4. let done = 0;
  5. let duration = 0;
  6. let hour, minute, second;
  7. let start = Date.now();
  8. let total = await models.Art.countDocuments();
  9. console.log('total:', total);
  10. models.Art
  11. .find()
  12. .sort({ lastMod: 1 })
  13. .cursor()
  14. .eachAsync(job)
  15. .catch(err => console.error(err.message))
  16. .then(() => require('process').exit(0))
  17. async function job(doc) {
  18. if (done >= firstOpenCount) doc.open = false;
  19. else doc.open = true;
  20. await doc.save();
  21. done++;
  22. duration = (Date.now() - start) / 1000;
  23. hour = (Math.floor(duration / 60 / 60)).toString().padStart(2, '0');
  24. minute = (Math.floor(duration / 60) % 60).toString().padStart(2, '0');
  25. second = (Math.floor(duration) % 60).toString().padStart(2, '0');
  26. console.log('progress: ' + Math.floor((100 * done / total)) + '% used time: ' + hour + ':' + minute + ':' + second);
  27. }
  28. }
  29. module.exports = { init }
  30. if (require.main == module) {
  31. init();
  32. }