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

Connection autoCreate option behaves differently migrating 5 to 6 #12940

Closed
2 tasks done
igrunert-atlassian opened this issue Jan 24, 2023 · 2 comments · Fixed by #13007
Closed
2 tasks done

Connection autoCreate option behaves differently migrating 5 to 6 #12940

igrunert-atlassian opened this issue Jan 24, 2023 · 2 comments · Fixed by #13007
Labels
confirmed-bug We've confirmed this is a bug in Mongoose and will fix it.
Milestone

Comments

@igrunert-atlassian
Copy link

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.8.4

Node.js version

18.13.0

MongoDB server version

4.12.1 (driver)

Typescript version (if applicable)

No response

Description

When upgrading from Mongoose 5.13.15 to 6.8.4, we switched to explicitly setting the autoCreate option to false on the connection. We previously never set the autoCreate option on our Schema objects, so they should default to null (which means use the connection's autoCreate option). Our Schemas instantiate before the connection is created.

We found that in Mongoose 6, the autoCreate commands buffered prior to the connection being created were executed, issuing createCollection commands to the database. This didn't happen previously in Mongoose 5.

Workaround was to explicitly set autoCreate to false on the Schema objects.

Steps to Reproduce

Added to test/connection.test.js

it('with autoCreate = false after schema create', async function() {
    const schema = new Schema({ name: String }, {
      collation: { locale: 'en_US', strength: 1 },
      collection: 'gh8814_Conn'
    });

    const Model = mongoose.model('gh8814_Conn', schema);

    const conn = mongoose.connect(start.uri, {
      autoCreate: false
    });

    await conn;
    await Model.init();

    const res = await mongoose.connection.db.listCollections().toArray();
    assert.ok(!res.map(c => c.name).includes('gh8814_Conn'));
  });

Expected Behavior

Schema with autoCreate: null option set follows the connection's autoCreate option, even if schema is initialized first and the createCollection command is buffered.

@IslandRhythms IslandRhythms added the confirmed-bug We've confirmed this is a bug in Mongoose and will fix it. label Jan 25, 2023
@IslandRhythms
Copy link
Collaborator

Just to double check, is your assertion suppose to pass or fail?

const mongoose = require('mongoose');

const testSchema = new mongoose.Schema({
  name: String
}, { collation: { locale: 'en_US', strength: 1 }, collection: 'gh8814_Conn' });

const Test = mongoose.model('gh8814_Conn', testSchema);

async function run() {
  await mongoose.connect('mongodb://localhost:27017', { autoCreate: false });
  await Test.init();
  const res = await mongoose.connection.db.listCollections().toArray();
  console.log(res);
  await mongoose.connection.dropDatabase();
};

run();

@igrunert-atlassian
Copy link
Author

The assertion fails, and I think the code should be changed to make the test pass.

The gh8814_Conn collection shouldn't be created (following the autoCreate option on the connection).

@vkarpov15 vkarpov15 added this to the 6.9.2 milestone Feb 6, 2023
lpizzinidev added a commit to lpizzinidev/mongoose that referenced this issue Feb 8, 2023
@vkarpov15 vkarpov15 modified the milestones: 6.9.2, 6.9.3 Feb 16, 2023
lpizzinidev added a commit to lpizzinidev/mongoose that referenced this issue Feb 20, 2023
lpizzinidev added a commit to lpizzinidev/mongoose that referenced this issue Feb 20, 2023
lpizzinidev added a commit to lpizzinidev/mongoose that referenced this issue Feb 21, 2023
vkarpov15 added a commit that referenced this issue Feb 21, 2023
fix(connection): disable createConnection buffering if autoCreate option is changed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
confirmed-bug We've confirmed this is a bug in Mongoose and will fix it.
Projects
None yet
3 participants