mongodbmongoosemongodb-update

Mongoose - Conditionally update fields in the update query


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"]} }}
)

Solution

  • An Aggregation Pipeline should be able to achieve it.

    requests:

    followers:

    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"
            }
          }
        }
      }
    ])
    

    Demo @ Mongo Playground