Automattic / mongoose

MongoDB object modeling designed to work in an asynchronous environment.
https://mongoosejs.com
MIT License
26.97k stars 3.85k forks source link

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

Closed moisesbites closed 3 years ago

moisesbites commented 3 years ago

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 commented 3 years ago

This is intended https://mongoosejs.com/docs/migrating_to_6.html#no-more-deprecation-warning-options

vkarpov15 commented 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.

moisesbites commented 3 years ago

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

moisesbites commented 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.

in fact mongoose is no longer creating indexes automatically when first loading data into a schema/model

vkarpov15 commented 3 years ago

@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 commented 3 years ago

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

IslandRhythms commented 3 years ago

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 commented 1 year ago

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