如何在mongodb中进行批量更新和删除操作?

huangapple go评论94阅读模式
英文:

How update and delete bulk operation in mongodb?

问题

我正在构建一个任务管理应用程序,只有管理员可以删除用户。当管理员删除一个用户时,我希望将被删除用户的任务分配给由他指定的用户。

  1. {
  2. _id: new ObjectId("63bad43ee5d01d4cc13d58b2"),
  3. title: '测试任务',
  4. completed: false,
  5. urgent: false,
  6. assignedBy: new ObjectId("63afc89d7e3da6e5d996d415"), // 例如:约翰
  7. asignTo: new ObjectId("63bac632f2f1830832d229a4"), // 例如:戴维
  8. __v: 0
  9. }

当我从数据库中删除戴维用户时,我希望将他的任务分配给约翰并保存任务。

  1. {
  2. _id: new ObjectId("63bad43ee5d01d4cc13d58b2"),
  3. title: '测试任务',
  4. completed: false,
  5. urgent: false,
  6. assignedBy: new ObjectId("63afc89d7e3da6e5d996d415"), // 例如:约翰
  7. asignTo: new ObjectId("63afc89d7e3da6e5d996d415"), // 例如:约翰
  8. __v: 0
  9. }
英文:

I'm building a taskmanagment application, where only admin can delete user. when admin delete a user, I want to assign deleted users task to the his assignby user.

  1. {
  2. _id: new ObjectId("63bad43ee5d01d4cc13d58b2"),
  3. title: 'Test task',
  4. completed: false,
  5. urgent: false,
  6. assignedBy: new ObjectId("63afc89d7e3da6e5d996d415"), // ex: john
  7. asignTo: new ObjectId("63bac632f2f1830832d229a4"), // ex: devid
  8. __v: 0
  9. }

when I delete devid user from the db I want to assign his task to the john and save the task

  1. {
  2. _id: new ObjectId("63bad43ee5d01d4cc13d58b2"),
  3. title: 'Test task',
  4. completed: false,
  5. urgent: false,
  6. assignedBy: new ObjectId("63afc89d7e3da6e5d996d415"), // ex: john
  7. asignTo: new ObjectId("63afc89d7e3da6e5d996d415"), // ex: john
  8. __v: 0
  9. }

答案1

得分: 0

我认为你正在寻找transactions以避免数据丢失。

因此,你可以尝试类似以下的代码(这不是真实的代码):

  1. const updateAndDelete = async (userToRemove) => {
  2. const session = await db.startSession()
  3. try {
  4. session.startTransaction();
  5. const user = await User.findById(userToRemove, {session: session})
  6. const {assignedBy} = user
  7. await user.remove({session: session})
  8. await Task.updateMany({asignTo: userToRemove}, {asignTo: assignedBy}, {session: session})
  9. await session.commitTransaction();
  10. } catch(e) {
  11. await session.abortTransaction();
  12. }
  13. session.endSession();
  14. }

如果一切正常,你可以提交事务并保存所有更改(你还可以添加一些 if 来抛出异常,如果需要的话),否则你可以回滚,不会有任何数据丢失。

英文:

I think you are looking for transactions to avoid loss data.

So you can try somethig like (not real code):

  1. const updateAndDelete = async (userToRemove) => {
  2. const session = await db.startSession()
  3. try {
  4. session.startTransaction();
  5. const user = await User.findById(userToRemove, {session: session})
  6. const {assignedBy} = user
  7. await user.remove({session: session})
  8. await Task.updateMany({asignTo: userToRemove}, {asignTo: assignedBy}, {session: session})
  9. await session.commitTransaction();
  10. } catch(e) {
  11. await session.abortTransaction();
  12. }
  13. session.endSession();
  14. }

If everything is ok, you commit the transaction and all changes are saved (also you can add some if an thorw an exception if you want), otherwise you can do a rollback and there is not any data loss.

huangapple
  • 本文由 发表于 2023年1月8日 23:14:24
  • 转载请务必保留本文链接:https://go.coder-hub.com/75048897.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定