Closed moisesbites closed 3 years ago
We'll need to update the docs you mentioned. Please remove the 'useCreateIndex' option when you upgrade to Mongoose 6, it is no longer necessary.
Ok. Thank you. But, how I can to force mongoose to create the indexes?
We'll need to update the docs you mentioned. Please remove the 'useCreateIndex' option when you upgrade to Mongoose 6, it is no longer necessary.
in fact mongoose is no longer creating indexes automatically when first loading data into a schema/model
@moisesbites Mongoose 6 does automatically create indexes, the useCreateIndex
option was to internally use createIndex()
instead of ensureIndex()
, which is a purely internal change.
Can you please provide a script that demonstrates Mongoose not creating indexes automatically?
@moisesbites Mongoose 6 does automatically create indexes, the
useCreateIndex
option was to internally usecreateIndex()
instead ofensureIndex()
, which is a purely internal change.Can you please provide a script that demonstrates Mongoose not creating indexes automatically?
I know this is closed, sorry. Thank you for your answer. Here is the example:
The "textIndex" in the processos collection, I needed to create by hand. The mondodb is a test database.
Unable to repro, the below script shows the index is created. Can you please modify the below script to demonstrate your issue?
const mongoose = require('mongoose');
const {Schema} = mongoose;
const testSchema = new Schema({
name: String,
age: Number,
weight: Number,
height: Number,
birthday: String
});
testSchema.index({age: 1}, {unique: true});
testSchema.index({ name: 1, weight: 1});
testSchema.index({ name: 1, birthday: 1}, {unique: true});
testSchema.index({ name: 1, height: 1});
testSchema.index({'$**': 'text'}, { name: 'textIndex'});
const Test = mongoose.model('Test', testSchema, 'test');
async function test() {
await mongoose.connect('mongodb://localhost:27017/test', {
useNewUrlParser: true,
useUnifiedTopology: true,
});
await mongoose.connection.dropDatabase();
await Test.init();
console.log(await Test.listIndexes());
}
test();
I had this issue as well, it was because autoIndex
in the ConnectOptions was set to true. Turning it off and the indexes are created:
mongoose.connect(process.env.COSMOSDB_CONNECTION_STRING, {
dbName: 'my-storage',
autoCreate: true,
autoIndex: true
})
Do you want to request a feature or report a bug? BUG
What is the current behavior?
"mongoose": "^6.0.2" is not creating indexes automaticly
If the current behavior is a bug, please provide the steps to reproduce.
I backed up a database and restored it into a new instance. After that, when testing the system on the new instance, I noticed that no index had been created on the shemas other than the "_id". After I updated mongoose, the options 'useCreateIndex', 'useFindAndModify' and 'useUnifiedTopology' were no more accepted in the connect method. Yhe https://mongoosejs.com/docs/connections.html#options page still seems to be out of date.
For example,
These indexes were not created for any schema.
What is the expected behavior?
The mongoose to create the indexes if they don't exist in the database.
What are the versions of Node.js, Mongoose and MongoDB you are using? Note that "latest" is not a version. mongoose: 6.0.2 nodejs: v16.3.0 mongodb: 5.0.2
Thank you so much for this great software.