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
Mongoose does not automatically create indexes after restoring a database. #10631
Comments
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? |
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 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
|
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.
The text was updated successfully, but these errors were encountered: