Mongoose does not automatically recreate index after reconnecting to different instance (calling .connection.close() then .connect()) #12047
Labels
enhancement
This issue is a user-facing general improvement that doesn't fix a bug or add a new feature
Milestone
Prerequisites
Mongoose version
6.1.4
Node.js version
16.14.2
MongoDB server version
5.3
Description
I have a test suite for my personal project where I use mongoose to connect to a new instance of MongoDB for each test case. The actual setup is complex, but following is the idea:
For one of the suites, I need to have text indexes built. However it seems like mongoose only automatically builds the text index when
.connect
is first called. It doesn't rebuild indexes for subsequent calls even if I am trying to connect to a completely different server.I suggest adding this caveat to
autoIndex
option documentation. Or make index automatically rebuild for each.connect
call prehaps?There is a solution to this, which is to simply call
ensureIndexes
manually. But this behaviour just seemed weird and thought it could be a bug.Steps to Reproduce
You just have to build a test suite or a script that does something similar to this (and make sure you use the index in
case1
andcase2
:I used
mongodb-memory-server
, but also reproduceable using multiple installs directly on the OS.Expected Behavior
Mongoose should automatically rebuild indexes after each
.connect
call if using different servers.The text was updated successfully, but these errors were encountered: