Open In App

Mongoose Query.prototype.regex() API

Last Updated : 11 Jan, 2023
Improve
Improve
Like Article
Like
Save
Share
Report

The Mongoose Query API.prototype.regex() method of the Mongoose API is used on the Query objects. It allows us to specify the regular expression. Using this method we can write the regex query condition to get the result set to accomplish our requirements. This method helps us to utilize regular expression capabilities for matching the string patterns in queries. Let us understand regex() method using an example.

Syntax:

query.regex( path, value );

Parameters: This method accepts two parameters as described below:

  • path: It is used to specify the path name from the collection in the form of string.
  • value: It is used to specify the regular expression pattern.

Return Value: This method does not return any value.

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 database present in the MongoDB.

 

Example 1: The below example illustrates the basic functionality of the Mongoose Connection regex() method. In this example, we are extracting the document whose name field ends with “ent2”. We have defined regular expression for the name field.

Filename: app.js

Javascript




// Require mongoose module
const mongoose = require("mongoose");
  
// Set Up the Database connection
  
const connectionObject = mongoose.createConnection(URI, {
    useNewUrlParser: true,
    useUnifiedTopology: true,
});
  
const studentSchema = new mongoose.Schema({
    name: { type: String },
    age: { type: Number },
    rollNumber: { type: Number },
});
  
const Student = connectionObject.model(
    'Student', studentSchema
);
  
const query = Student.find()
query.regex("name", /ent2$/);
query.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("63a4a9a207370cdcd1961b2c"),
    name: 'Student2',
    age: 18,
    rollNumber: 176,
    __v: 0
  }
]

Example 2: The below example illustrates the basic functionality of the Mongoose Connection regex() method. When we provide one single argument then we have to use regex() method after where() method and the most recent path provided to where method is used.

Filename: app.js

Javascript




// Require mongoose module
const mongoose = require("mongoose");
  
// Set Up the Database connection
  
const connectionObject = mongoose.createConnection(URI, {
    useNewUrlParser: true,
    useUnifiedTopology: true,
});
  
const studentSchema = new mongoose.Schema({
    name: { type: String },
    age: { type: Number },
    rollNumber: { type: Number },
});
  
const Student = connectionObject.model('Student', studentSchema);
  
const query = Student.find()
query.where('name').regex(/^St/);
query.exec((error, result) => {
    if (error) {
        console.log(error);
    } else {
        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("63a40a1065e8951038a391b1"),
    name: 'Student1',
    age: 30,
    rollNumber: 9,
    __v: 0
  },
  {
    _id: new ObjectId("63a4a98407370cdcd1961b1a"),
    name: 'Student3',
    age: 33,
    rollNumber: 178,
    __v: 0
  },
  {
    _id: new ObjectId("63a4a9a207370cdcd1961b2c"),
    name: 'Student2',
    age: 18,
    rollNumber: 176,
    __v: 0
  }
]

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



Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads