我有以下查询,它从文档中的两个数组(请求和关注者)中提取id
:
User.updateOne(
{ _id: otherUserId },
{ $pull: { requests: userId, followers: userId } }
)
我想通过使用一个条件来修改这个查询,说明userId
是否在requests
数组中,然后从requests
but don't update the followers array中拉取.如果id
不在requests
中,则从followers
中拉出.目前,无论如何,这两个字段都会更新.有什么方法可以使用一个查询来实现这一点吗?
我try 了下面的代码,但它返回了CastError
User.updateOne(
{ _id: otherUserId },
{ $pull: { requests: userId, followers: {$nin: [new mongoose.Types.ObjectId(userId), "$requests"]} }}
)