Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can mongodb create index when inserting data?

Tags:

mongodb

I've read the docs of mongodb and get to know that if I want to do text search I should use create index. But can I create index for each data during insert process? If I can, how should I do?

like image 817
Coding_Rabbit Avatar asked Oct 14 '25 08:10

Coding_Rabbit


1 Answers

The index is created once for a collection, i.e. for text index do this

db.yourCollection.createIndex({yourText:"text"})

The index is updated automatically on every insert operation. You don't have to do this manually but have it in mind. This is what makes insert operations expensive. The more indexes you have the longer it takes to insert a document and update all of them.

If you want a link to the documentation, this topic is faced here.

like image 197
Gerald Mücke Avatar answered Oct 17 '25 01:10

Gerald Mücke



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!