-
-
Notifications
You must be signed in to change notification settings - Fork 3.9k
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
Comments
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 But this is not true. This only means it does not create the indices when you call Should this also be implict in the docs somewhere? |
In addition, https://mongoosejs.com/docs/guide.html#autoIndex mentions Lots of "in addition" for this documentation :) |
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 |
@vkarpov15 I see it creating indexes on |
@thernstig no, that isn't intentional. We'll take a look and see if we can repro that. |
@vkarpov15 I did some better research. It seems to be the package we use that creates the indices: I think this can be closed then again, but leaving it open for your to decide. |
@thernstig thanks for letting me know. I took a look and confirmed that
|
Hi If anyone still facing this issue, Please make sure that Example: This is incorrect. It will create the schema even if the flags are set as false `
` But this is correct:
` |
https://mongoosejs.com/docs/guide.html#autoCreate says:
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:So from the logs, the collection is created. But the above text in the docs it should not be.
The text was updated successfully, but these errors were encountered: