How to fix MongoDB not creating Index

How to fix MongoDB not creating Index

·

1 min read

Today I went into a typical MongoDB Issue again. When I created a Model and wrote a population Script, the Index never got applied and the Population was really slow. Gladly I found a fix.

Using syncIndexes()

The mongoose Libary has a Method to manual sync Local Indexes with the Ones in the Document in the MongoDB Collection. It is called syncIndexes() and you can call it on each Model to ensure that the local Index is applied to the remote ones.

Changing some Options

Now when we populate the Indexes of the Documents by Hand we can disable some default behaviour in Mongoose. In the options of the connect() Method there is a property called autopopulate which should populate all Indexes on creation of a Collection with Mongoose, somehow this never worked out for me so I recommend to do it the manual way, so we can set this property to false now.

Hope you enjoyed this little tutorial, if you have anything what I could improve please write a comment. Jannik