Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Mongoose does not automatically recreate index after reconnecting to different instance (calling .connection.close() then .connect()) #12047

Closed
2 tasks done
junzhengca opened this issue Jul 3, 2022 · 0 comments · Fixed by #12130
Labels
enhancement This issue is a user-facing general improvement that doesn't fix a bug or add a new feature
Milestone

Comments

@junzhengca
Copy link

junzhengca commented Jul 3, 2022

Prerequisites

  • I have written a descriptive issue title
  • I have searched existing issues to ensure the bug has not already been reported

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:

describe('suite', () => {
     beforeEach(() => {
        const uri = getNewMongoUri();
        mongoose.connect(uri);
     });

     afterEach(() => {
        mongoose.connection.close();
     });


     it('case1'); //...
     it('case2'); //...
});

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 and case2:

describe('suite', () => {
     beforeEach(() => {
        const uri = getNewMongoUri();
        mongoose.connect(uri);
     });

     afterEach(() => {
        mongoose.connection.close();
     });


     it('case1'); //...
     it('case2'); //...
});

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.

@vkarpov15 vkarpov15 modified the milestones: 6.4.4, 6.4.5 Jul 7, 2022
@IslandRhythms IslandRhythms added the enhancement This issue is a user-facing general improvement that doesn't fix a bug or add a new feature label Jul 13, 2022
@vkarpov15 vkarpov15 modified the milestones: 6.4.5, 6.4.6, 6.4.7 Jul 15, 2022
vkarpov15 added a commit that referenced this issue Jul 20, 2022
@vkarpov15 vkarpov15 modified the milestones: 6.4.6, 6.5 Jul 20, 2022
vkarpov15 added a commit that referenced this issue Jul 26, 2022
fix(connection): re-run `Model.init()` if re-connecting after explicitly closing a connection
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement This issue is a user-facing general improvement that doesn't fix a bug or add a new feature
Projects
None yet
3 participants