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

Mongoose does not automatically create indexes after restoring a database. #10631

Closed
moisesbites opened this issue Aug 27, 2021 · 8 comments
Closed
Labels
can't reproduce Mongoose devs have been unable to reproduce this issue. Close after 14 days of inactivity.

Comments

@moisesbites
Copy link

moisesbites commented Aug 27, 2021

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.

image

For example,

schema.index({ name: 1 });

schema.index(
  { '$**': 'text' },
  {
    name: 'textIndex',
    default_language: 'portuguese'
  }
);

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

image

Thank you so much for this great software.

@IslandRhythms
Copy link
Collaborator

@IslandRhythms IslandRhythms added the help This issue can likely be resolved in GitHub issues. No bug fixes, features, or docs necessary label Aug 30, 2021
@vkarpov15 vkarpov15 added this to the 6.0.3 milestone Aug 30, 2021
@vkarpov15 vkarpov15 added docs This issue is due to a mistake or omission in the mongoosejs.com documentation and removed help This issue can likely be resolved in GitHub issues. No bug fixes, features, or docs necessary labels Aug 30, 2021
@vkarpov15
Copy link
Collaborator

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.

@vkarpov15 vkarpov15 modified the milestones: 6.0.3, 6.0.4 Aug 30, 2021
@moisesbites
Copy link
Author

moisesbites commented Aug 31, 2021

Ok. Thank you. But, how I can to force mongoose to create the indexes?

@moisesbites
Copy link
Author

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

@vkarpov15
Copy link
Collaborator

@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
Copy link
Author

moisesbites commented Sep 1, 2021

@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?

I know this is closed, sorry. Thank you for your answer. Here is the example:

image

image

image

The "textIndex" in the processos collection, I needed to create by hand. The mondodb is a test database.

@vkarpov15 vkarpov15 reopened this Sep 2, 2021
@vkarpov15 vkarpov15 modified the milestones: 6.0.4, 6.0.6 Sep 2, 2021
@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 Sep 2, 2021
@IslandRhythms IslandRhythms added can't reproduce Mongoose devs have been unable to reproduce this issue. Close after 14 days of inactivity. and removed needs repro script Maybe a bug, but no repro script. The issue reporter should create a script that demos the issue labels Sep 7, 2021
@IslandRhythms
Copy link
Collaborator

IslandRhythms commented Sep 7, 2021

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

@truonghongtrieu
Copy link

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
can't reproduce Mongoose devs have been unable to reproduce this issue. Close after 14 days of inactivity.
Projects
None yet
Development

No branches or pull requests

4 participants