Open In App

Mongoose Document.prototype.$op API

Last Updated : 12 Dec, 2022
Improve
Improve
Like Article
Like
Save
Share
Report

The Document API.prototype.$op property of the Mongoose API is used on the Document model. It allows us to determine the ongoing operation mongoose in performing or executing on the document object. It may be null, save, validate or remove. Let us understand the $op property using an example.

Syntax:

document.$op;

Parameters: This property does not accept any parameter.

Return Value: This property returns a string value. It returns the current operation that is executing on the document object.

Setting up Node.js Mongoose Module:

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: 

 

Database Structure: The database structure will look like this, the following documents are present in the collection.

 

Example 1: In this example, we have established a database connection using mongoose and defined model over userSchema, having five columns or fields “_id”, “name”, “fixedDeposit”, “interest”, and “tenure”. At the end, we are removing this documet and using $op property we are displaying ongoing operation.

Filename: app.js

Javascript




// Require mongoose module
const mongoose = require("mongoose");
  
// Set Up the Database connection
    useNewUrlParser: true,
    useUnifiedTopology: true,
});
  
const userSchema = new mongoose.Schema({
    name: String,
    fixedDeposit: Number,
    interest: Number,
    tenure: { type: Number, default: 6 }
});
  
const User = mongoose.model('User', userSchema);
  
User.findOne({ _id: '639180857bb3df8284caef2b' })
    .exec((error, document) => {
        if (error) {
            console.log(error);
        } else {
            document.remove();
            console.log(document.$op)
        }
    })


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

node app.js

Output:

remove

GUI Representation of the Database using Robo3T GUI tool:

 

Example 2: In this example, we are using validate() method on the document object, and to know the ongoing operation, we are using $op property and displaying the output on the console.

Filename: app.js

Javascript




// Require mongoose module
const mongoose = require("mongoose");
  
// Set Up the Database connection
    useNewUrlParser: true,
    useUnifiedTopology: true,
});
  
const userSchema = new mongoose.Schema({
    name: String,
    fixedDeposit: Number,
    interest: Number,
    tenure: { type: Number, default: 6 }
});
  
const User = mongoose.model('User', userSchema);
  
const newDoc = new User(
    { "name": "Eric", "fixedDeposit": 5000,
      "interest": 0.03, "tenure": 15, }
);
newDoc.validate();
console.log(newDoc.$op)


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

node app.js

Output:

validate

Reference: https://mongoosejs.com/docs/api/document.html#document_Document-$op



Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads