masfmqowkf
masfmqowkf

Reputation: 121

mongodb won't delete an element from ObjectId array

My schema looks like the following:

const userSchema = new Schema({
  ...
  followings: [
      {
          user:{ 
              type: Schema.ObjectId, 
              ref: 'User' 
          },
      }

  ],
  followers: [
      {
          user:{ 
              type: Schema.ObjectId, 
              ref: 'User' 
          },
      }
  ],
}, {timestamps: true})

And I need to implement "unfollow" feature. I am currently trying this:

try {

            // check if your id doesn't match the id of the user you want to unfollow
            if (user._id === current_id) {
                return res.status(400).json({ error: 'You cannot unfollow yourself' })
            }

            // remove the id of the user you want to unfollow from following array
            const query = {
                _id: current_id
            }

            const update = {
                $pull: { "followings": {"_id": user._id }}
            }

            const updated = User.update(query, update)

            // remove your id from the followers array of the user you want to unfollow
            const secondQuery = {
                _id: user._id
            }

            const secondUpdate = {
                $pull: { "followers": {"_id": current_id} }
            }

            const secondUpdated = User.update(secondQuery, secondUpdate)

            if (!updated || !secondUpdated) {
                return res.status(404).json({ error: 'Unable to unfollow that user' })
            }

            res.status(200).json({
                update, 
                secondUpdate
            })
} 
catch (err) {
            res.status(400).send({ error: err.message })
}

This gives status 200 and sends update & secondUpdate to the client, but the actual object doesn't get deleted from the database. What is wrong with my code?

Upvotes: 0

Views: 494

Answers (2)

masfmqowkf
masfmqowkf

Reputation: 121

Thank you for your answers. In my case, the following worked for me.

try {

            // check if your id doesn't match the id of the user you want to unfollow
            if (user._id === current_id) {
                return res.status(400).json({ error: 'You cannot unfollow yourself' })
            }

            // remove the id of the user you want to unfollow from following array
            const query = {
                _id: current_id
            }

            const update = {
                $pull: { followings: {_id: user._id }}
            }

            const updated = User.updateOne(query, update, {
                safe: true
            }, function(err, obj) {
                console.log(err);
            })

            // remove your id from the followers array of the user you want to unfollow
            const secondQuery = {
                _id: user._id
            }

            const secondUpdate = {
                $pull: { followers: {_id: current_id} }
            }

            console.log(secondQuery)
            console.log(secondUpdate)

            User.updateOne(secondQuery, secondUpdate, {
                safe: true
              }, function(err, obj) {
                res.status(200).json({
                    obj
                });
              })

} 
catch (err) {
            res.status(400).json({ error: err.message })
}

Upvotes: 0

Danizavtz
Danizavtz

Reputation: 3270

If you are using mongodb native drive

You should import mongodb ObjectID. Before performing operations.

const ObjectId = require('mongodb').ObjectID;

$pull: { "followers": {"_id": current_id} }

change to:

$pull: { "followers": {"_id": new ObjectId(current_id) }

Upvotes: 1

Related Questions