question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Docs: Schema option 'autoCreate' default

See original GitHub issue

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.

Issue Analytics

  • State:closed
  • Created 2 years ago
  • Comments:8

github_iconTop GitHub Comments

8reactions
karankumarslicepaycommented, Feb 1, 2022

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);

`

1reaction
vkarpov15commented, Jan 10, 2022

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

Read more comments on GitHub >

github_iconTop Results From Across the Web

Mongoose v6.8.1: Defaults
Your schemas can define default values for certain paths. If you create a new document without that path set, the default will kick...
Read more >
Documentation: 15: 5.9. Schemas - PostgreSQL
Keep the default search path, and grant privileges to create in the public schema. All users access the public schema implicitly. This simulates...
Read more >
Specifying a schema | BigQuery - Google Cloud
In the Explorer panel, expand your project and select a dataset. Expand the more_vert Actions option and click Open. In the details panel,...
Read more >
86. Database Initialization - Spring
Spring Boot chooses a default value for you based on whether it thinks your database is embedded. It defaults to create-drop if no...
Read more >
Schema and data type mapping in copy activity - Microsoft Learn
Such default mapping supports flexible schemas and schema drift ... If you want to create a templatized pipeline to copy large number of ......
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found