"MongoDB Getting Started Tutorial" Chapter 28 Delete Index

This article will introduce the dropIndex() method of MongoDB to delete the index.

dropIndex() method

The dropIndex() method of the collection can be used to delete the index, the syntax is as follows:

db.collection.dropIndex(index)

Among them, index represents the index to be deleted, which can be a string specifying the name of the index, or a document describing the definition of the index.

Note that the default index on the _id field cannot be dropped.

dropIndex() Example

Example 1: Delete a single index

First, create an index based on the "Release Date" field in the movies collection using the following command:

db.movies.createIndex({
    
    'Release Date': 1})

'Release Date_1'

The above statement creates an index named "".

Second, use the getIndexes() method to view all indexes in the movies collection:

db.movies.getIndexes()

[
  {
    
     v: 2, key: {
    
     _id: 1 }, name: '_id_' },
  {
    
     v: 2, key: {
    
     Title: 1 }, name: 'Title_1' }
  {
    
     v: 2, key: {
    
     'Release Date': 1 }, name: 'Release Date_1' }
]

Next, drop the index "Release Date_1" using dropIndex():

db.movies.dropIndex('Release Date_1')

{
    
     nIndexesWas: 3, ok: 1 }

The returned result shows that an index has been deleted.

Finally, use the getIndexes() method again to view the indexes:

db.movies.getIndexes()

[
  {
    
     v: 2, key: {
    
     _id: 1 }, name: '_id_' },
  {
    
     v: 2, key: {
    
     Title: 1 }, name: 'Release Date_1' }
]

Example 2: Delete an index based on a definition

First, recreate the index based on the "Release Date" field:

db.movies.createIndex({
    
    'Release Date': 1})

'Release Date_1'

Look at the index on the movies collection:

db.movies.getIndexes()

[
  {
    
     v: 2, key: {
    
     _id: 1 }, name: '_id_' },
  {
    
     v: 2, key: {
    
     Title: 1 }, name: 'Title_1' }
  {
    
     v: 2, key: {
    
     'Release Date': 1 }, name: 'Release Date_1' }
]

Then, drop the index "Release Date_1" based on its definition:

db.movies.dropIndex({
    
    'Release Date': 1});

{
    
     nIndexesWas: 3, ok: 1 }

Example 3: Drop all non-primary key indexes

Starting with MongoDB 4.2, it is not possible to drop all non-primary key (_id) indexes using a collection's dropIndex('*') , instead you need to use the dropIndexes() method:

db.collection.dropIndexes()

First, recreate the index based on the "Release Date" field:

db.movies.createIndex({
    
    'Release Date': 1})

'Release Date_1'

Look at the index on the movies collection:

db.movies.getIndexes()

[
  {
    
     v: 2, key: {
    
     _id: 1 }, name: '_id_' },
  {
    
     v: 2, key: {
    
     Title: 1 }, name: 'Title_1' }
  {
    
     v: 2, key: {
    
     'Release Date': 1 }, name: 'Release Date_1' }
]

Finally, use the dropIndexes() method to drop indexes other than _id:

db.movies.dropIndexes()

{
    
    
  nIndexesWas: 3,
  msg: 'non-_id indexes dropped for collection',
  ok: 1
}

Guess you like

Origin blog.csdn.net/horses/article/details/129201370