简体   繁体   中英

How to create index properly in MongoDB to search in entire one collection?

I a have collection named people and this collection have almost 100k documents ..

While I am a front-end dev, I don't know How can I boost my search performance.

I do search as below:

const phoneNumbers = [
   { "phone": { "$regex": `1` } },
   { "phone": { "$regex": `2` } },
   { "phone": { "$regex": `3` } },
   { "phone": { "$regex": `4` } },
   { "phone": { "$regex": `5` } },
   { "phone": { "$regex": `xxx` } },
   { "phone": { "$regex": `999` } },
   { "phone": { "$regex": `1000` } },
];


peopleCollection.find({$or: phoneNumbers}).toArray((error, matchedDocs)=> {
   // returning matched docs
});

As you can see the structure of my search what is your suggestions to create index ?

Is that helpful to create index ? If yes how Can I create a proper index ?

Indexing does not reduce the complexity of your search expression, rather it helps the database to find out the matched result faster(ie reduce the time complexity).

Here is how you can create indexing:

db.collectionName.createIndex(key_name)

The above one-line operation creates indexing on key key_name .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM