Varun Nayyar
Varun Nayyar

Reputation: 897

Indexing in mongoose documents

I a working on a search feature in which I have to search over multiple fields in a documents.

I have following cases:

1) Get all documents where field1,field2,field3,field4 matches the string (nayyar)

2) Get all documents where field1,field2,field3,field4 matches the string (nayyar) AND gender should be (input value) or 'N/A'.

3) Get all documents where field1,field2,field3,field4 matches the string (nayyar) AND age should be between ageHigh and ageLow AND gender should be (input value) or 'N/A'.

4) Get all documents where field1,field2,field3,field4 matches the string (nayyar) AND age should be between ageHigh and ageLow AND gender should be (input value) or 'N/A' AND ArrayField in 'atoz'.

var Schema = new Schema({
lowAge          :   {
                        type: Number, 
                        default: 0 
                    },
highAge         :   {
                        type: Number, 
                        default: 0
                    },
gender          :   {
                        type: String,
                        default: 'N/A'
                    },
field1          :   {
                        type: String
                    },
field2          :   {
                        type: String
                    },
field3          :   {
                        type: String
                    },
field4          :   {
                        type: String
                    },
arrayField      :   {
                        type: [String]
                    }
});


Schema.index({field1:1, field2:1, field3:1, field4:1});
Schema.index({field1:1, field2:1, field3:1, field4:1, arrayField:1,lowAge:1, highAge:1, gender:1,});

Is it the correct way to index ?

Upvotes: 0

Views: 1347

Answers (1)

notionquest
notionquest

Reputation: 39166

Firstly, the idea of creating the index on the fields seem alright. However, creating index on all possible fields used in the query doesn't look correct.

All the queries listed in the post contain the four fields. If you create a compound index on all these 4 fields, I think it should be sufficient.

Also, you are defining the index at the schema level. When the application starts up, Mongoose automatically calls ensureIndex for each defined index in your schema. This could cause some performance impact.

You need to consider the following:-

1) Single field index i.e. creating four indexes one on each field.

Example:

db.collection.createIndex({field1 : 1});

Go for this option if you would need to find documents by either field1, field2, field3 or field4 or combination of one or more fields.

In this options, the indexes are independent. MongoDB will always end up using an index (i.e. one of the indexes).

2) Compound index i.e. creating one index with four fields

Example:

db.collection.createIndex({field1 : 1, field2 : 1, field3 : 1, field4 :1});

Go for this option if you would have field1 always.

If you don't have field1 in any scenario, the MongoDB wouldn't use index.

Upvotes: 5

Related Questions