remove()
is a Mongoose function that removes a given path or paths from a document. It removes the path based on the passed argument.
A path is a field in a document present in a database or collection.
Schema.remove(path)
path
: this is the path you want to be removed.
A schema instance is returned.
// import mongooseconst mongoose = require("mongoose");// creating a product Schema with mongooselet Product = mongoose.model("Item", new mongoose.Schema({name: String,category: Number,price: Number,tag: String}))// check if product Educative.io// exists with the exists() methodlet product = await Product.remove("category");console.log(product.path("category")) // Undefined
In the example below, we create a Product Schema and remove the category
path with the remove()
function. Then we log out the removed path to see if it still exists.
After the successful removal of the “category” path, Undefined
is returned when you try and find the path again.