我正在编写一个相当大的迁移并且有这个代码(coffeescript):
db.users.find().forEach (user)->
try
#some code changing the user depending on the old state
db.users.save(user)
print "user_ok: #{user._id}"
catch error
print "user_error: #{user._id}, error was: #{error}"
发生了一些错误。但它们发生在已处理的用户身上:
user_ok: user_1234
#many logs
user_error: user_1234 ...
循环如何获取已处理的对象?
我最终做了:
backup = { users: [] }
db.users.find().forEach (user)->
try
#some code changing the user depending on the old state
backup.users.push user
print "user_ok: #{user._id}"
catch error
print "user_error: #{user._id}, error was #{error}"
#loop backup and save
现在效果很好,但看起来真的很奇怪。请问这一切背后的意义何在?