Open In App

Mongoose Query.prototype.then() API

Last Updated : 27 Oct, 2022
Improve
Improve
Like Article
Like
Save
Share
Report

Mongoose is an Object Data Modeling (ODM) library for MongoDB. It defines a strongly-typed schema, with default values and schema validations which are later mapped to a MongoDB document. 

The Mongoose Query API.prototype.then() method returns a Promise after executing the query. If any occurs while performing the particular operation, it will be handled by the catch block.

Syntax:

query.then(resolve).catch(reject)

 

Parameters:

  • resolve: A function that gets returned when there is no error.
  • reject: A function that gets returned after any error.

Return type: This method returns a promise.

Installation of mongoose module:

Step 1: You can visit the link to Install the mongoose module. You can install this package by using this command.

npm install mongoose

Step 2: After installing the mongoose module, you can check your mongoose version in the command prompt using the command.

npm version mongoose

Step 3:  After that, you can just create a folder and add a file for example index.js, To run this file you need to run the following command.

node index.js

Project Structure: The project structure will look like this:

 

Sample Collection: Below is the sample data in the database before the function is executed, You can use any GUI tool or terminal to see the database like we have used the MongoDB compass GUI tool as shown below:

 

Example 1:  In this example, we are retrieving all the customers whose interests are in boxing.

Javascript




// Importing the module 
const mongoose = require('mongoose');
  
// Set Up the Database connection
mongoose.connect(
    useNewUrlParser: true,
    useUnifiedTopology: true
})
  
  
// Defining customerSchema schema
const customerSchema = new mongoose.Schema(
    { name: String, interest: Array, orderCount: Number }
)
  
  
// Defining customerSchema model
const Customer = mongoose.model(
    'Customer', customerSchema);
  
//Finding the record in the collection 
Customer.find({ interest: "boxing" }).then((res) => {
    console.log(res)
});


Steps to run the application: Run the index.js file using the below command:

node index.js

 

Example 2:  In this example, we are deliberately creating an error by passing String in the find method (It takes an object to run perfectly) and catching it using .catch( ).

Javascript




// Importing the module
const mongoose = require('mongoose');
  
// Set Up the Database connection
mongoose.connect(
    useNewUrlParser: true,
    useUnifiedTopology: true
})
  
  
// Defining customerSchema schema
const customerSchema = new mongoose.Schema(
    { name: String, interest: Array, orderCount: Number }
)
  
  
// Defining customerSchema model
const Customer = mongoose.model(
    'Customer', customerSchema);
  
  
Customer.find("boxng").then((res) => {
    console.log(res)
}).catch((err) => {
    console.log("Man, You got an error")
    console.log(err)
});


Step to run the application: Run the index.js file using the below command:

node index.js

 

Reference:  https://mongoosejs.com/docs/api/query.html#query_Query-then



Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads