Mongoose: ensureIndex is deprecated starting with MongoDB 3.0.0

Created on 17 Aug 2015  路  17Comments  路  Source: Automattic/mongoose

I tried searching the issues but I couldn't find anything about it, and I saw that the code still uses ensureIndex, so I thought I should give a heads up about this deprecation in MongoDB. :)

http://docs.mongodb.org/manual/reference/method/db.collection.ensureIndex/

Most helpful comment

You're right, we'll change back to ensureIndex() for now and undeprecate until I can figure out what's going on on the mongodb side. Sorry about the trouble, I mistakenly assumed the docs were accurate without actually verifying.

All 17 comments

I think this can just be replaced by createIndex from what it seems. Could probably be addressed in 4.x still as it wouldn't be a breaking change.

Yeah since createIndex is just an alias for ensureIndex (see docs ) we'll switch over in 4.x.

This breaks for us. Apparently createIndex is not just an alias for ensureIndex?

We get Index with name: ... already exists with different options even though nothing changed.

MongoDB version is 3.2.10 (Compose).

That's strange, because it is supposed to be:

https://docs.mongodb.com/manual/reference/method/db.collection.ensureIndex/

db.collection.ensureIndex(keys, options)
Deprecated since version 3.0.0: db.collection.ensureIndex() is now an alias for db.collection.createIndex().

@djanowski sorry for the trouble. Can you provide me some more information on how the indexes were created? And also open a separate issue?

As a workaround, you can disable autoIndex and just rely on your indexes as they are right now.

In our case, we did see the same thing that @djanowski reports after the update, caused by a 'safe' option being set in the index options. See #3439. Since this shows up only in development for us, we just dropped the indices and allowed them to auto-create again.

However, this might be indicative of a more serious issue, if the commentary in this SE discussion about 3.7 refusing to restore if 'safe' is present is true:

https://stackoverflow.com/questions/31464332/in-a-mongodb-index-what-do-the-options-safe-and-force-mean

Considering that MongoDB 3.6 is not out yet, I doubt there's anything that's set in stone for 3.8, so I don't trust that comment re: 3.7. But please provide code samples to help us reproduce this :+1:

@vkarpov15 The indices in question all had safe: null set, placed there by by an autoindex using mongoose < #3439. I'm not sure how to repro on the levels we're at now, frankly.

I can confirm that the safe option isn't important for indexes, you can remove it. Sorry about the inconvenience.

Hi @djanowski,

I am facing with the same problem "Index with name: ... already exists with different options" and I have the MongoDB in Compose too (version 3.2.10).

My problem has started when I've upgraded Mongoose from 4.8.5 to 4.12.2.

Thanks in advance

Please provide code samples @codeback

Hi @vkarpov15, thank you for your response.

Mongoose Options:

options: {
            autoIndex: false,
            auth: { authdb: 'admin' },
            user: '<user>'
            pass: '<password>',
            db: {
                safe: true
            },
            mongos: {
                ssl: true,
                sslValidate: true,
                sslCA: ca
            }
        }

Mongoose connection:

...
db = mongoose.createConnection(uri, options);
...

Model:

...
const ProviderSchema = new mongoose.Schema({
    _modifiedAt: { type: Date, default: Date.now },
    _createdAt: { type: Date, default: Date.now },
    _modifiedBy: { type: Schema.Types.ObjectId, ref: 'User'},
    _createdBy: { type: Schema.Types.ObjectId, ref: 'User'},
    kind: { type: String, enum: kindOptions, required: true },
    domain: { type: String, required: true, unique: true },
    ...
});

// Indexes
ProviderSchema.index({ kind: 1});
ProviderSchema.index({ domain: 1});

// Virtuals

// Validations

const Provider = db.model('Provider', ProviderSchema);

I am also running into IndexOptionsConflict.

Looking at the code of node-mongodb-driver it is clear that it is not a simple alias.

createIndex

https://github.com/mongodb/node-mongodb-native/blob/3.0.0/lib/db.js#L1029

ensureIndex

https://github.com/mongodb/node-mongodb-native/blob/3.0.0/lib/db.js#L1113

For one. ensureIndex checks if the index already exists.

@Moeriki I've raised an issue for that on their JIRA boards:

https://jira.mongodb.org/browse/NODE-1167

Wonder what they come back with, because this is clearly a conflict between the docs and the code. I would trust the docs thought, if they state ensureIndex is deprecated. It will probably be removed in a future version.

It's worth pointing out that MongoDB driver still uses ensureIndex in various places.

Fun side effect: using mongodb's GridFS on a connection created by mongoose causes ensureIndex() is deprecated in Mongoose >= 4.12.0 warning to appear because of this code inside mongodb driver.

You're right, we'll change back to ensureIndex() for now and undeprecate until I can figure out what's going on on the mongodb side. Sorry about the trouble, I mistakenly assumed the docs were accurate without actually verifying.

Goes to show how important it is to have accurate documentation, if a lot of people rely on your code :)

I wonder what they'll come back with at MongoDB.

Was this page helpful?
0 / 5 - 0 ratings