mongoose+koa2 按照_id更新多条数据,删除数组中的字段,然后添加新的字段,$pull和$or结合使用

时间:2023-03-09 01:56:30
mongoose+koa2 按照_id更新多条数据,删除数组中的字段,然后添加新的字段,$pull和$or结合使用

await model.photo.update({
_id: {
$in: photoIdsParam
}
}, {
$pull: {
customerIds: {
code: custCode,
$or: [{
'userIds.0': {
$exists: false
}
}, {
'userIds.0': ctx.user.userid
}]
}
}
}, {
multi: true
})

await model.photo.update({
_id: {
$in: photoIdsParam
}
}, {
$addToSet: {
customerIds: {
userIds: ctx.user.userid,
code: custCode,
isDel: 'true'
}
}
}, {
upsert: true,
multi: true
})