I have the following query which pulls an id
from two arrays (requests and followers) in the document:
User.updateOne(
{ _id: otherUserId },
{ $pull: { requests: userId, followers: userId } }
)
I want to modify this query by using a condition stating if the userId
is in the requests
array, then pull from requests
but don't update the followers array. If the id
is not in requests
, then pull from followers
. Currently, both fields are updated regardless. Any way to accomplish this using one query?
I tried the following code, however it returned a CastError
User.updateOne(
{ _id: otherUserId },
{ $pull: { requests: userId, followers: {$nin: [new mongoose.Types.ObjectId(userId), "$requests"]} }}
)
An Aggregation Pipeline should be able to achieve it.
requests
:
$filter
- Filter the requests
array to remove the userId
if it exists.followers
:
$cond
- Check userId
is not in the requests
array.true
, remove the userId
from the followers
array.followers
array.User.updateOne(
{ _id: otherUserId },
[
{
$set: {
requests: {
$filter: {
input: "$requests",
cond: {
$ne: [
"$$this",
userId
]
}
}
},
followers: {
$cond: {
if: {
$not: {
$in: [
userId,
"$requests"
]
}
},
then: {
$filter: {
input: "$followers",
cond: {
$ne: [
"$$this",
userId
]
}
}
},
else: "$followers"
}
}
}
}
])