Mongoose on('delete') - onlinecode

Mongoose on(‘delete’)

Mongoose on(‘delete’)

In this post, we will give you information about Mongoose on(‘delete’) – onlinecode. Here we will give you detail about Mongoose on(‘delete’) – onlinecode And how to use it also give you a demo for it if it is necessary.

Mongoose lets you register middleware on delete functions.
This lets you add extra checks or other business logic that Mongoose executes every time someone calls a deleteOne() or deleteMany().
To enable this feature, you must register them on the desired schema like so:

const aSchema = new mongoose.Schema({
  testId: {
    type: Schema.Types.ObjectId,
    ref: 'Test'
  },
  name: String
});

aSchema.pre('deleteOne', function() {
  console.log('Before deleteOne');
});
aSchema.pre('deleteMany', function() {
  console.log('Before deleteMany');
});

const A = mongoose.model('A', aSchema);

await A.deleteOne(); // Prints "Before deleteOne"
await A.deleteMany(); // Prints "Before deleteMany"

this

In deleteOne() and deleteMany() middleware, this is the Mongoose Query object, not the document(s) being deleted.

aSchema.pre('deleteOne', function() {
  this instanceof mongoose.Query; // true
});
aSchema.pre('deleteMany', function() {
  this instanceof mongoose.Query; // true
});

Keep in mind that Mongoose registers deleteOne() and deleteMany() middleware on Query.prototype.deleteOne() and Query.prototype.deleteMany() by default.
That means Document.prototype.deleteOne() fires deleteOne() middleware, but only because Document.prototype.deleteOne() calls Query.prototype.deleteOne().

const testSchema = new mongoose.Schema({
  name: String
});
testSchema.pre('deleteOne', async function() {
  console.log(this instanceof mongoose.Query); // true
});
const Test = mongoose.model('Test', testSchema);

const doc = await Test.create({});

await doc.deleteOne(); // Prints "true"

You can make deleteOne() middleware fire with this as the document being deleted by setting the { document: true } option on pre() and post() as shown below.
However, keep in mind that, if you set { document: true, query: false }, your middleware will only fire on Document.prototype.deleteOne().
It won’t fire on Query.prototype.deleteOne().

const testSchema = new mongoose.Schema({
  name: String
});
testSchema.pre('deleteOne', { document: true, query: false }, async function() {
  console.log(this instanceof mongoose.Document); // true
});
const Test = mongoose.model('Test', testSchema);

const doc = await Test.create({});

await doc.deleteOne(); // Prints "true"

await Test.deleteOne(); // Doesn't print anything

Change Streams for Mongoose on(‘delete’)

Mongoose will only fire middleware if the delete operation goes through Mongoose.
For example, the previous section’s middleware won’t fire if someone deletes a document through the MongoDB shell, Studio 3T, or an app written in Java.
You must use the change streams feature in order to detect changes from other apps as shown below:

const testSchema = new mongoose.Schema({
  name: String
});

const Test = mongoose.model('Test', testSchema);

Test.watch().on('change', (data) => {
  // check if it is a delete operation
  if (data.operationType == 'delete') {
    // do stuff
  }
});

You must be connected to a MongoDB replica set or sharded cluster to use change streams.


A mongoose is a small, carnivorous mammal that is found in Africa, Asia, and southern Europe. They are known for their ability to kill venomous snakes, and they have been used for centuries to control snake populations. Mongooses are also popular pets, but they can be difficult to care for and are not recommended for everyone.

Here are some of the pros and cons of owning a mongoose:

Pros:

  • Mongooses are intelligent and can be trained to do tricks.
  • They are relatively small and easy to care for.
  • They can be effective at controlling snake populations.

Cons:

  • Mongooses can be aggressive and unpredictable.
  • They are not legal to own in some places.
  • They can carry diseases, such as rabies.

If you are considering getting a mongoose as a pet, it is important to do your research and make sure that you are prepared to provide the proper care. Mongooses are not for everyone, but they can make great companions for the right people.

Here are some additional facts about mongooses:

  • They are about the size of a cat, with long bodies and tails.
  • They have sharp claws and teeth, which they use to catch and kill prey.
  • They are active during the day and night.
  • They eat a variety of foods, including snakes, rodents, insects, and fruit.
  • Mongooses live in a variety of habitats, including forests, grasslands, and deserts.
  • They are solitary animals, but they will sometimes gather in groups to hunt.
  • Mongooses are monogamous, and they mate for life.
  • Females give birth to litters of 2-4 young.
  • Mongooses reach sexual maturity at about 6 months old.
  • They have a lifespan of about 10 years.

Hope this code and post will helped you for implement Mongoose on(‘delete’) – onlinecode. if you need any help or any feedback give it in comment section or you have good idea about this post you can give it comment section. Your comment will help us for help you more and improve us. we will give you this type of more interesting post in featured also so, For more interesting post and code Keep reading our blogs

For More Info See :: laravel And github

We're accepting well-written guest posts and this is a great opportunity to collaborate : Contact US