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

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

How update and delete bulk operation in mongodb?

问题

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

{
  _id: new ObjectId("63bad43ee5d01d4cc13d58b2"),
  title: '测试任务',
  completed: false,
  urgent: false,
  assignedBy: new ObjectId("63afc89d7e3da6e5d996d415"), // 例如:约翰
  asignTo: new ObjectId("63bac632f2f1830832d229a4"), // 例如:戴维
  __v: 0
}

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

{
  _id: new ObjectId("63bad43ee5d01d4cc13d58b2"),
  title: '测试任务',
  completed: false,
  urgent: false,
  assignedBy: new ObjectId("63afc89d7e3da6e5d996d415"), // 例如:约翰
  asignTo: new ObjectId("63afc89d7e3da6e5d996d415"), // 例如:约翰
  __v: 0
}
英文:

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.

{
  _id: new ObjectId("63bad43ee5d01d4cc13d58b2"),
  title: 'Test task',
  completed: false,
  urgent: false,
  assignedBy: new ObjectId("63afc89d7e3da6e5d996d415"), // ex: john
  asignTo: new ObjectId("63bac632f2f1830832d229a4"), // ex: devid
  __v: 0
}

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

{
  _id: new ObjectId("63bad43ee5d01d4cc13d58b2"),
  title: 'Test task',
  completed: false,
  urgent: false,
  assignedBy: new ObjectId("63afc89d7e3da6e5d996d415"), // ex: john
  asignTo: new ObjectId("63afc89d7e3da6e5d996d415"), // ex: john
  __v: 0
}

答案1

得分: 0

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

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

const updateAndDelete = async (userToRemove) => {
    const session = await db.startSession()
    try {
        session.startTransaction();
        const user = await User.findById(userToRemove, {session: session})
        const {assignedBy} = user
        await user.remove({session: session})
        await Task.updateMany({asignTo: userToRemove}, {asignTo: assignedBy}, {session: session})
        await session.commitTransaction();
    } catch(e) {
        await session.abortTransaction();
    }
    session.endSession();
}

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

英文:

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

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

const updateAndDelete = async (userToRemove) => {
    const session = await db.startSession()
    try {
        session.startTransaction();
        const user = await User.findById(userToRemove, {session: session})
        const {assignedBy} = user
        await user.remove({session: session})
        await Task.updateMany({asignTo: userToRemove}, {asignTo: assignedBy}, {session: session})
        await session.commitTransaction();
    } catch(e) {
        await session.abortTransaction();
    }
    session.endSession();
}

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:

确定