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

Docs: Schema option 'autoCreate' default #11116

Closed
thernstig opened this issue Dec 17, 2021 · 8 comments
Closed

Docs: Schema option 'autoCreate' default #11116

thernstig opened this issue Dec 17, 2021 · 8 comments
Labels
help This issue can likely be resolved in GitHub issues. No bug fixes, features, or docs necessary

Comments

@thernstig
Copy link

https://mongoosejs.com/docs/guide.html#autoCreate says:

Before Mongoose builds indexes, it calls Model.createCollection() to create the underlying collection in MongoDB if autoCreate is set to true.

In our setup, we are using the defaults for this as well as autoIndex. I.e. we are using this:
autoIndex: true (the default)
autoCreate: false (the default)

Still when our mongoose code starts running and when e.g. db1.model('Proxy', proxySchema, 'proxy'); is called we can see this in the mongodb logs:

2021-12-16T18:16:17.245+0100 I STORAGE  [conn95] createCollection: db1.proxy with generated UUID: 434da758-b375-4195-ab14-47f519c49458
2021-12-16T18:16:17.600+0100 I INDEX    [conn95] build index on: db1.proxy properties: { v: 2, key: { time: -1 }, name: "time_-1", ns: "db1.mtasAlarms", background: true }

So from the logs, the collection is created. But the above text in the docs it should not be.

@thernstig
Copy link
Author

In addition, reading https://mongoosejs.com/docs/guide.html does not give a clear indication on when indices and collections will be created if you set autoIndex to false.

I found out that if you set autoIndex to false with mongoose.set('autoIndex', false), one could think it never creates the indices.

But this is not true. This only means it does not create the indices when you call mongoose.model (or connection.model). But it does create them as soon as you make a query towards the model/collection.

Should this also be implict in the docs somewhere?

@thernstig
Copy link
Author

In addition, https://mongoosejs.com/docs/guide.html#autoIndex mentions mongoose.set('autoIndex', false) but https://mongoosejs.com/docs/guide.html#indexes never mentions this in the example text.

Lots of "in addition" for this documentation :)

@IslandRhythms IslandRhythms added the docs This issue is due to a mistake or omission in the mongoosejs.com documentation label Dec 17, 2021
@vkarpov15 vkarpov15 added this to the 6.1.6 milestone Dec 23, 2021
@vkarpov15
Copy link
Collaborator

Thanks for the suggestions, we've applied most of them.

Re: "But this is not true. This only means it does not create the indices when you call mongoose.model (or connection.model). But it does create them as soon as you make a query towards the model/collection.", I don't think that's correct. The below script shows that no schema indexes are created:

'use strict';
  
const mongoose = require('mongoose');
const { Schema } = mongoose;

mongoose.set('debug', true);

run().catch(err => console.log(err));

async function run() {
  await mongoose.connect('mongodb://localhost:27017/test');
  
  await mongoose.connection.dropDatabase();
  
  let schema = new Schema({ name: String }, { autoCreate: true, autoIndex: false });
  schema.index({ name: 1 });
  let Test = mongoose.model('Test', schema);
  await Test.init();

  await Test.findOne();

  // Prints "[ { v: 2, key: { _id: 1 }, name: '_id_' } ]"
  console.log(await Test.collection.listIndexes().toArray());
} 

Perhaps you're wondering about why the _id index shows up - the MongoDB server creates an _id index on every collection when the collection is created, Mongoose has nothing to do with that.

@thernstig
Copy link
Author

@vkarpov15 I see it creating indexes on expires if I use that, but maybe that is also intentional? It just threw me off as autoIndex seems to indicate no indices are created if set to false. Should/could it be mentioned that some indices are still created even if it set to false?

@vkarpov15
Copy link
Collaborator

@thernstig no, that isn't intentional. We'll take a look and see if we can repro that.

@vkarpov15 vkarpov15 reopened this Jan 10, 2022
@vkarpov15 vkarpov15 modified the milestones: 6.1.6, 6.1.7 Jan 10, 2022
@vkarpov15 vkarpov15 added needs repro script Maybe a bug, but no repro script. The issue reporter should create a script that demos the issue and removed docs This issue is due to a mistake or omission in the mongoosejs.com documentation labels Jan 10, 2022
@thernstig
Copy link
Author

@vkarpov15 I did some better research. It seems to be the package we use that creates the indices:

https://github.com/animir/node-rate-limiter-flexible/blob/3cdb40bd4cbf78eb551c7d68dcf89f25934cf9a3/lib/RateLimiterMongo.js#L102-L109

I think this can be closed then again, but leaving it open for your to decide.

@vkarpov15
Copy link
Collaborator

@thernstig thanks for letting me know. I took a look and confirmed that expires indexes don't get created if you set autoIndex: false as shown below, so I think we can close this.

'use strict';
  
const mongoose = require('mongoose');

mongoose.set('autoIndex', false);

const { Schema } = mongoose;

run().catch(err => console.log(err));

async function run() {
  await mongoose.connect('mongodb://localhost:27017/test');

  await mongoose.connection.dropDatabase();

  const schema = new Schema({ test: { type: Date, expires: '1 minute' } });
  const Model = mongoose.model('Test', schema);

  await Model.init();

  console.log(await Model.listIndexes());
}

@vkarpov15 vkarpov15 removed this from the 6.1.7 milestone Jan 16, 2022
@vkarpov15 vkarpov15 added help This issue can likely be resolved in GitHub issues. No bug fixes, features, or docs necessary and removed needs repro script Maybe a bug, but no repro script. The issue reporter should create a script that demos the issue labels Jan 16, 2022
@karankumarslicepay
Copy link

Hi

If anyone still facing this issue, Please make sure that
mongoose.set('autoIndex', false) and mongoose.set('autoCreate', false);
should have been set before any schema creation

Example: This is incorrect. It will create the schema even if the flags are set as false

`

"use strict";

const mongoose = require("mongoose");
const { Schema } = mongoose;
mongoose.set("debug", true);

run().catch((err) => console.log(err));

async function run() {
    let schema = new Schema({ name: String });
    schema.index({ name: 1 });
    let Test = mongoose.model("Test", schema);
    mongoose.connect("mongodb://localhost:27017/test");
    mongoose.set("autoCreate", false);   // <<<<<----- not after mongoose.model
    mongoose.set("autoIndex", false);    // <<<<<----- not after mongoose.model
    mongoose.connection.on("connected", async function () {
        console.log("Mongoose default connection is open.");
        try {
            await Test.init();
            await Test.findOne();

            // Prints "[ { v: 2, key: { _id: 1 }, name: '_id_' } ]"
            console.log(await Test.collection.listIndexes().toArray());
        } catch (e) {
            console.error({ e });
        }
    });
    mongoose.connection.on("error", function (err) {
        console.log("Mongoose default connection has occurred " + err + " error");
    });
    mongoose.connection.on("disconnected", function () {
        console.log("Mongoose default connection is disconnected");
    });
}

setTimeout(() => {
    console.log("settimout");
}, 60000);

`

But this is correct:
`

"use strict";

const mongoose = require("mongoose");
const { Schema } = mongoose;
mongoose.set("debug", true);
mongoose.set("autoCreate", false); // <<<<<----- correct, before mongoose.model
mongoose.set("autoIndex", false); // <<<<<----- correct, before mongoose.model
run().catch((err) => console.log(err));

async function run() {
    let schema = new Schema({ name: String });
    schema.index({ name: 1 });
    let Test = mongoose.model("Test", schema);
    mongoose.connect("mongodb://localhost:27017/test");
    mongoose.connection.on("connected", async function () {
        console.log("Mongoose default connection is open.");
        try {
            await Test.init();
            await Test.findOne();

            // Prints "[ { v: 2, key: { _id: 1 }, name: '_id_' } ]"
            console.log(await Test.collection.listIndexes().toArray());
        } catch (e) {
            console.error({ e });
        }
    });
    mongoose.connection.on("error", function (err) {
        console.log("Mongoose default connection has occurred " + err + " error");
    });
    mongoose.connection.on("disconnected", function () {
        console.log("Mongoose default connection is disconnected");
    });
}

setTimeout(() => {
    console.log("settimout");
}, 60000);

`

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
help This issue can likely be resolved in GitHub issues. No bug fixes, features, or docs necessary
Projects
None yet
Development

No branches or pull requests

4 participants