Skip to content
Related Articles
Get the best out of our app
GFG App
Open App
geeksforgeeks
Browser
Continue

Related Articles

Mongoose Schemas Indexes

Improve Article
Save Article
Like Article
Improve Article
Save Article
Like Article

Mongoose is a MongoDB object modeling and handling for a node.js environment. When a document is produced, Mongoose automatically uses the _id property to index all of the models. We can use Mongoose to define the path-level indexes in our schema. For each defined index in the schema, Mongoose automatically runs createIndex when the application first launches.

Creating node application and Installing Mongoose:

Step 1: Create a node application using the following command:

mkdir folder_name
cd folder_name
npm init -y

Step 2: Install the required module using the following command:

npm install mongoose

Project Structure: It will look like the following.

 

Example 1: In this example, we will create some documents and see the default indexes, which is set by the mongoose automatically.

Javascript




// Require the mongoose module
const mongoose = require('mongoose');
 
// Path to our Database
 
// Connecting to database
mongoose.connect(url)
    .then((ans) => {
        console.log("Connected  successfully")
    })
    .catch((err) => {
        console.log("Error in the Connection")
    })
 
// Calling Schema class
const Schema = mongoose.Schema;
 
// Creating Structure of the collection
const collection_structure = new Schema({
    name: String,
    marks: Number,
})
 
// Creating collection
const collections = mongoose.model("GFG", collection_structure)
 
collections.create({
    name: `Ragavpp`,
    marks: 13,
})
    .then((ans) => {
        console.log("Document inserted")
    })
 
    .catch((err) => {
        console.log(err.Message);
    })


Step to Run Application: Run the application using the following command from the root directory of the project:

node script.js

Output:

Connected Successful
Document inserted

Following we can see the default indexes in Atlas GUI: 

indexes output

Example 2:  In this example, we will create indexes with mobile_No. 

Javascript




// Require the mongoose module
const mongoose = require('mongoose');
 
// Path to our cloud DataBase
 
// Connecting to database
mongoose.set('strictQuery', false);
 
mongoose.connect(url)
    .then((ans) => {
        console.log("Connected Successful")
    })
    .catch((err) => {
        console.log("Error in the Connection")
    })
 
// Calling Schema class
const Schema = mongoose.Schema;
 
// Creating Structure of the collection
// And making indexes using mobile
const collection_structure = new Schema({
    name: {
        type: String,
        require: true,
    },
    marks: {
        type: Number,
        default: 0
    },
    mobile: {
        type: Number,
        index: true,
        require: true,
    }
})
 
// Creating collection
const collections = mongoose.model("GFG", collection_structure)
 
collections.create({
    name: `Sam Snehil`,
    marks: 88,
    mobile: 9338948473,
})
    .then((ans) => {
        console.log("Document inserted")
    })
 
    .catch((err) => {
        console.log(err);
    })


Step to Run Application: Run the application using the following command from the root directory of the project:

node script2.js

Step to Run Application: Run the application using the following command from the root directory of the project:

node main.js

Output:

 

Following we can see the mobile in indexes on Atlas GUI:

index output2

Reference: https://mongoosejs.com/docs/guide.html#indexes   


My Personal Notes arrow_drop_up
Last Updated : 20 Jan, 2023
Like Article
Save Article
Similar Reads
Related Tutorials