Open In App

Mongoose Document Model.exists() API

Last Updated : 09 Sep, 2022
Improve
Improve
Like Article
Like
Save
Share
Report

The Model.exists() method of the Mongoose API is used to find documents in the database. This method returns a document with “_id” if at least one document is present in the database that matches the given filter condition.

Syntax:

Model.exists()

Parameters: The Model.exists() method accepts four parameters:

  • filter: It is an object which specifies the condition to select the document from the collection.
  • options: It is an object with various properties.
  • callback: It is a callback function that will run once execution is completed.

Returns: The Model.exists() function returns a promise. The result contains an object with “_id” of the document that matches the filter condition. If not matched it will return null.

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:

 

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 two columns or fields “name” and “age”. In the end, we are using exists() method on the User model to get the “_id” of the document where the value of “age” field is “20”. 

  • 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);
User.exists({age: 20}).then(result => {
    console.log(result)
})


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

node app.js

Output:

{ _id: new ObjectId("6316eef52d1b8030ebe17040") }

Example 2: In this example, we are providing such value to the “age” field for which the document does not exist in the database. So, in the output, we will get a “null” value.

  • 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);
User.exists({age: 50}).then(result => {
    console.log(result)
})


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

node app.js

Output:

null

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



Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads