skip to Main Content

i would like to store a lot of data in the database. So I hope that I can use the index to do the filtering.

My current idea is, when I apply the index, mongodb only search for the data with the same index. So, it can save a lot time and resources.

Below is my data that i want to save into database

{ "name": "Mr A","type": "Individual","country": "Nepal"}

{ "name": "Mr B","type": "Individual","country": "Singapore"}

{ "name": "Company C","type": "Corporate","country": "Singapore"}

So when i save, how do I create index when i save the date?

const customer = new Customer({ "name": "Mr A","type": "Individual,"country": "Nepal"});

await customer.save()

const customer2 = new Customer({ "name": "Mr B","type": "Individual","country": "Singapore"});

await customer2.save()

const customer3 = new Customer({ "name": "Company C","type": "Corporate","country": "Singapore"});

await customer3.save()

// Create index now and how?

// hope to create index 

So, when i do the search

const findCustomer = await Customer.find({type: "Individual"}).explain("executionStats")

I hope that, mongodb only "totalDocsExamined" only 2 document instead of 3

2

Answers


  1. You can create an index using the ensureIndexes() method, you can create an index on the "type" field.

    const mongoose = require('mongoose');
    
    // Define your schema
    const customerSchema = new mongoose.Schema({
      name: String,
      type: String,
      country: String,
    });
    
    // Create an index on the 'type' field
    customerSchema.index({ type: 1 });
    
    // Create a model using the schema
    const Customer = mongoose.model('Customer', customerSchema);
    
    // Now, save your documents
    const customer = new Customer({ name: "Mr A", type: "Individual", country: "Nepal" });
    await customer.save();
    
    const customer2 = new Customer({ name: "Mr B", type: "Individual", country: "Singapore" });
    await customer2.save();
    
    const customer3 = new Customer({ name: "Company C", type: "Corporate", country: "Singapore" });
    await customer3.save();
    
    // Ensure the indexes are created
    await Customer.ensureIndexes();
    
    // Now, when you search for customers of a specific type, the index will be used
    const findCustomer = await Customer.find({ type: "Individual" }).explain("executionStats");
    console.log(findCustomer);
    
    Login or Signup to reply.
  2. Indexes can be defined on the path where you declare each property like so:

    const customerSchema = new mongoose.Schema({
      name: String,
      type: {
          type: String,
          index: true
      },
      country: String
    });
    

    Or you can define them on the schema like so:

    const customerSchema = new mongoose.Schema({
      name: String,
      type: String,
      country: String
    });
    customerSchema.index({ type: 1 });
    

    Be careful with the type property you have declared. In mongoose type has a special meaning in a schema and can be the source of unexpected behaviour if you don’t understand the data correctly.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search