What is the recommended way to drop indexes using Mongoose?

17,602

Solution 1

To do this via the Mongoose model for the collection, you can call dropAllIndexes of the native collection:

MyModel.collection.dropAllIndexes(function (err, results) {
    // Handle errors
});

Update

dropAllIndexes is deprecated in the 2.x version of the native driver, so dropIndexes should be used instead:

MyModel.collection.dropIndexes(function (err, results) {
    // Handle errors
});

Solution 2

If you want to maintain your indexes in your schema definitions with mongoose (you probably do if you're using mongoose), you can easily drop ones not in use anymore and create indexes that don't exist yet. You can just run a one off await YourModel.syncIndexes() on any models that you need to sync. It will create ones in the background with .ensureIndexes and drop any that no longer exist in your schema definition. You can look at the full docs here: https://mongoosejs.com/docs/api.html#model_Model.syncIndexes

Solution 3

It looks like you're attempting to drop all of the indexes on a given collection.

According to the MongoDB Docs, this is the correct command.

... I tried to use executeDbCommand adapted from this post, but with no success:

To really help here, we need more details:

  • What failed? How did you measure "no success"?
  • Can you confirm 100% that the command ran? Did you output to the logs in the callback? Did you check the err variable?
  • Where are you creating indexes? Can you confirm that you're not re-creating them after dropping?
  • Have you tried the command while listing specific index names? Honestly, you should not be using "*". You should be deleting and creating very specific indexes.
Share:
17,602
npclaudiu
Author by

npclaudiu

Updated on June 12, 2022

Comments

  • npclaudiu
    npclaudiu almost 2 years

    I need to create several deployment scripts like data migration and fixtures for a MongoDB database and I couldn't find enough information about how to drop indexes using Mongoose API. This is pretty straight-forward when using the official MongoDB API:

    To delete all indexes on the specified collection:

    db.collection.dropIndexes();

    However, I would like to use Mongoose for this and I tried to use executeDbCommand adapted from this post, but with no success:

    mongoose.connection.db.executeDbCommand({ dropIndexes: collectionName, index: '*' },
      function(err, result) { /* ... */ });
    

    Should I use the official MongoDB API for Node.js or I just missed something in this approach?

  • npclaudiu
    npclaudiu over 11 years
    By "no success" I mean that the code executed without any errors, but the callback provided by me was never called. I have this problem in a small script whose only responsibility is to drop and create certain collections, including their associated indexes and fill those collections with some data needed for testing.
  • J.Wolfe
    J.Wolfe almost 4 years
    OP wants to use mongoose - while you can get at native driver code like this through mongoose, it's not the simplest or most straightforward way.
  • kr094
    kr094 about 3 years
    For folks coming from Google searches for "Mongoose remove duplicate index" this is the desired solution. It uses the mongoose APIs instead of the accepted answer.
  • Anurag pareek
    Anurag pareek over 2 years
    This solution looks exactly what I want and also should be accepted answer