Deleting documents in MongoDB
Similar to updateOne()
and updateMany()
, there are the two class methods, namely deleteOne(conditions, callback)
and deleteMany(conditions, callback)
that allow you to delete the first document (deleteOne()
) or all the documents (deleteMany()
) that satisfy the conditions expressed.
In addition, the instance method doc.remove(callback)
also makes it possible to delete the doc
document when it is in memory.
Let’s remove Clinton
from the collection by using the deleteOne()
method, then display the new contents of the collection:
Using deleteOne() to delete client “Clinton” (test.js file)
var mongoose = require("mongoose"); mongoose.connect("mongodb://localhost/mydb_test"); var clientSchema = mongoose.Schema({ lastname : String, firstname : String, address : String }); // creation of the Client class associated with the clients // collection var Client = mongoose.model("clients", clientSchema...