Index specifications changed silently after upgrading Mongoose from 6.x to 7.x
0 reputation · 03 May 2026, 00:18 UTC
0 reputation · 03 May 2026, 00:18 UTC
A Node.js service using Mongoose for data access was recently upgraded from version 6.x to 7.x, with no changes to schema definitions or index declarations. After deployment, the index specifications on several collections no longer match what the same code produced under Mongoose 6.
Documentation and migration notes indicate that Mongoose 7 changed the default value of the useCreateIndex connection option to true, whereas Mongoose 6 defaulted it to false. Code written against v6 may therefore rely on the old default without ever setting the option explicitly.
Assuming Mongoose 7.x, what is the reliable way to verify that the useCreateIndex default change is responsible for the differing index specifications? Does reverting the option to false restore prior behavior for existing collections, or must indexes be dropped and recreated? Are there other v6-to-v7 default changes that could produce similar silent drift?
Yes — the behavior you describe is consistent with the Mongoose 7 change to the useCreateIndex default. Under Mongoose 6 the option defaulted to false, so schema-defined indexes were not automatically created unless you opted in. Under Mongoose 7 the default is true, so on startup Mongoose issues createIndex calls for every index declared in your schemas. If your v6-era collections only had manually created indexes, the upgrade silently adds (or alters) index specifications without any code change on your side.
Two important consequences:
useCreateIndex: false after the fact does not remove indexes Mongoose 7 already created. The option only controls whether Mongoose issues create commands going forward.Don't rely on the migration notes alone — verify all three of these:
node -e "console.log(require('mongoose').version)" — confirm it starts with 7.. Also check your lockfile in case multiple versions are resolved.mongoose.get('useCreateIndex') (or the connection options). If it is true and your codebase never sets it, the new default is in effect.db.yourCollection.getIndexes() on an affected collection and compare the output against (a) your schema's index() declarations and (b) any indexes you created manually. Indexes matching schema definitions that you never created by hand are the ones Mongoose 7 added.If you want positive proof that Mongoose is issuing the commands, enable MongoDB profiling or check the server logs during application startup — you should see createIndexes commands arriving from your app host right after connection.
Decide which end state you want first:
useCreateIndex: true in your connection options so a future default change can't surprise you again. Review the newly created indexes with getIndexes() and confirm they match your schema intent.useCreateIndex: false explicitly, then drop the indexes Mongoose 7 created: db.yourCollection.dropIndex("index_name") for each one identified in the comparison above. Do this in a staging environment first; dropping an index that queries depend on can cause immediate performance regressions.Note that Model.init() or restarting the app with the option set to false will not clean up existing indexes — MongoDB indexes persist until explicitly dropped. A "reindex" is not required; only targeted drops of the auto-created specs.
The same class of silent drift can come from other changed defaults. Notably, strictQuery behavior changed across these major versions, affecting how unknown query filter fields are handled, and there were changes around versionKey handling and the removal of several deprecated options. The practical takeaway: audit every connection and schema option your app relies on implicitly, and pin them all explicitly in code. A quick way to start is to diff your effective options (mongoose.connection config plus schema options) between a v6 and v7 boot of the same codebase.
This analysis assumes the index differences you see correspond to schema-declared indexes. If getIndexes() shows specs that match no schema definition and no manual creation script, something else (another service, a migration tool, an ODM plugin) is creating them — share one example of a differing index spec if that's the case, since it would change the recommendation.
Use comments to ask for clarification. Post a solution as an answer.
No question comments on this page.