I
I
ikerya2020-07-28 08:36:56
Node.js
ikerya, 2020-07-28 08:36:56

Why doesn't mongoose call createIndex?

I don't understand why mongoose doesn't call createIndex on the new model. In a collection in the mongo shell, the indexes specified in the model schema are not displayed (because they are not created); there is only one index on the _id field.

I deleted the collection in the moongo shell more than once and tried to check again, but all in vain. I also tried to remove useCreateIndex in the mongoose config, but it doesn’t even give me a deprecation warning, like ensureIndex is deprecated, use createIndex instead.

In the mongo shell, the index is quietly created via createIndex, but that's not the point. Sense then from the mongoose scheme ..

Options when connecting to the mongoose:

{
    "useNewUrlParser": true,
    "useFindAndModify": false,
    "useCreateIndex": true,
    "useUnifiedTopology": true
}

Model file (mongoose.connect not included in the example):

const { Schema, model } = require('mongoose');
const schema = new Schema({
    telegramId: { type: Number, required: true, unique: true }
});
    
module.exports = model('User', schema);

Answer the question

In order to leave comments, you need to log in

1 answer(s)
I
ikerya, 2020-07-29
@ikerya

I also declared methods on this model, one of which was named init , and essentially overridden the mongoose init system method , which resulted in this error.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question