Categories
JavaScript Answers

How to delete a key from a MongoDB document using Mongoose?

Spread the love

Sometimes, we want to delete a key from a MongoDB document using Mongoose.

In this article, we’ll look at how to delete a key from a MongoDB document using Mongoose.

How to delete a key from a MongoDB document using Mongoose?

To delete a key from a MongoDB document using Mongoose, we can use the schema update method.

For instance, we write

User.update({
  _id: user._id
}, {
  $unset: {
    field: 1
  }
}, callback);

to call update on the entry with the given _id value.

Then we use the $unset operator to remove the field property from the User entry.

The callback is a function that runs when the update operation is done.

Conclusion

To delete a key from a MongoDB document using Mongoose, we can use the schema update method.

By John Au-Yeung

Web developer specializing in React, Vue, and front end development.

Leave a Reply

Your email address will not be published. Required fields are marked *