The Wayback Machine - https://web.archive.org/web/20240812203855/https://www.geeksforgeeks.org/mongoose-document-model-events-api/
Open In App

Mongoose Document Model.events API

Last Updated : 15 Sep, 2022
Comments
Improve
Suggest changes
Like Article
Like
Save
Share
Report
News Follow

The Model.events property of Mongoose API is used to return the model’s events instance. The instance of the event contains the fields like _events, _eventsCount, and _maxListeners.

Syntax:

Model_Name.events

Return Value: The Model.events property returns the events instance of the model.

Setting up Node.js application:

Step 1: Create a Node.js application using the following command:

npm init

Step 2: After creating the NodeJS application, Install the required module using the following command:

npm install mongoose

Project Structure: The project structure will look like this:

 

Example 1: In this example, we have established a database connection using mongoose and defined model over userSchema, having two columns or fields “name” and “age”. In the end, we are using the events property on the User model which will return the events instance of the model.

  • app.js: Write down the below code in the app.js file:

Javascript




// Require mongoose module
const mongoose = require('mongoose');
  
// Set Up the Database connection
mongoose.connect(
    useNewUrlParser: true,
    useUnifiedTopology: true
})
  
const userSchema = new mongoose.Schema(
    { name: String, age: Number }
)
  
// Defining userSchema model
const User = mongoose.model('User', userSchema);
  
// Getting events property
const output = User.events
console.log("Events instance is:",output)


Steps to run the program: To run the application execute the below command from the root directory of the project:

node app.js

Output:

Events instance is: EventEmitter {
  _events: [Object: null prototype] {},
  _eventsCount: 0,
  _maxListeners: undefined,
  [Symbol(kCapture)]: false
}

Example 2: In this example, we have established a database connection using mongoose and defined model over studentSchema, having three columns or fields “name”, “father_name”, and “mother_name”. In the end, we are using events property on the Student model which will return events instance of the model with an event on it.

  • app.js: Write down the below code in the app.js file:

Javascript




// Require mongoose module
const mongoose = require('mongoose');
  
// Set Up the Database connection
mongoose.connect(
useNewUrlParser: true,
useUnifiedTopology: true
})
  
const studentSchema = new mongoose.Schema(
{ name: String, father_name:
String, mother_name: String }
)
  
// Defining studentSchema model
const Student = mongoose.model('Student', studentSchema);
  
// Setting the events property
Student.events.on('error', err => console.log(err.message));
  
// Accessing base property on Student model
const output = Student.events
console.log("Events Instance is:", output)


Steps to run the program: To run the application execute the below command from the root directory of the project:

node app.js

Output:

Events Instance is: EventEmitter {
  _events: [Object: null prototype] { error: [Function (anonymous)] },
  _eventsCount: 1,
  _maxListeners: undefined,
  [Symbol(kCapture)]: false
}

Reference: https://mongoosejs.com/docs/api/model.html#model_Model-events



Previous Article
Next Article

Similar Reads

Mongoose Document Model.prototype.model() API
The Mongoose Document API Model.prototype.model() method of the Mongoose API is used on the Document model. It allows to get the model instance. We can call this method on any model object and by providing another model name to the method parameter we will get the new instance of that model. Let us understand the model() method using an example. Sy
3 min read
Mongoose Document Model.prototype.baseModelName API
The Model.prototype.baseModelName property of the Mongoose API is used to return the baseModelName as the name of the base model, if this is a discriminator model, Syntax: Model_Name.baseModelName Return Value: The Model.prototype.baseModelName property returns the baseModelName is the name of the base model, Setting up Node.js application: Step 1:
2 min read
Mongoose Document Model.updateOne() API
The Model.updateOne() method of the Mongoose API is used to update documents in a collection. This method will update the very first document that will match the filter irrespective of the value of the multi-option. Model.updateOne() method accepts four parameters: filter: It is an object to filter out a document that needs to be updated.update: It
3 min read
Mongoose Document Model.countDocuments() API
The Model.countDocuments() method of the Mongoose API is used to count the number of document present in a collection. The countDocument() method count the number of documents in a collection based on matching filter. Model.countDocuments() method accepts four parameters: filter: It is an object to filter out a document that needs to be updated.cal
3 min read
Mongoose Document Model.where() API
The Model.where() method of the Mongoose API is used to extract documents from a collection based on conditions. We can directly use where() on any model and on where() we can put various relational conditions to get the result. Model.where() returns array of objects. Each object consists of the document in a collection. Setting up Node.js applicat
3 min read
Mongoose Document Model.bulkWrite() API
The Model.bulkWrite() method of the Mongoose API is used to perform multiple operations in one command. It can insert multiple documents, can update one or multiple documents, can replace documents, can delete documents in one single command. This is faster in performance than multiple independent create commands. Syntax: Model.bulkWrite() Paramete
3 min read
Mongoose Document Model.create() API
The Model.create() method of the Mongoose API is used to create single or many documents in the collection. Mongoose by default triggers save() internally when we use the create() method on any model. Syntax: Model.create() Parameters: The Model.create() method accepts three parameters: docs: It is an object of key-value pair which will be inserted
3 min read
Mongoose Document Model.replaceOne() API
The Model.replaceOne() method of the Mongoose API is used to replace any one document in a collection. This method works the same as the update method but it replaces MongoDB's existing document with the given document with any atomic operator i.e $set. Syntax: Model.replaceOne() Parameters: The Model.replaceOne() method accepts four parameters: fi
3 min read
Mongoose Document Model.remove() API
The Model.remove() method of the Mongoose API is used to remove all the documents present in the collection in one go. It will be called on a Collection model which is already defined and present in the database. Syntax: Model.remove() Parameters: The Model.remove() method accepts two parameters: options: It is an object with various properties.cal
3 min read
Mongoose Document Model.prototype.deleteOne() API
The Model.prototype.deleteOne() method of the Mongoose API is used to delete any one document in a collection. This method deletes the first document that matches the condition provided to the method as a first parameter. Syntax: Model.prototype.deleteOne() Parameters: The Model.prototype.deleteOne() method accepts four parameters: condition: It is
3 min read
three90RightbarBannerImg