MongoDB index management-Create, view, delete __mongodb the index

Source: Internet
Author: User

Http://itbilu.com/database/mongo/E1tWQz4_e.html

Indexing is the most effective means to improve query query efficiency. An index is a special kind of data structure, indexes store part of the data in an easy to traverse form (such as a specific field or set of field values), the index sorts the stored values by certain rules, and the index is stored in memory where it is very fast to retrieve data from the index. If there is no index, MONGODB must scan each document in the collection, which is very inefficient, especially when the volume of data is large. Create/Rebuild Index View index Delete index


1. Create/Rebuild Index

MongoDB The new creation Index uses the Ensureindex () method, which can be rebuilt with reindex () for an existing index. 1.1 Creating an Index Ensureindex ()

MongoDB Create an index using the Ensureindex () method.

Grammatical structure

Db. Collection_name.ensureindex (Keys[,options])
Keys, the list of parameters to establish the index. For example: {key:1}, where KEY represents the field name, 1 is sorted in ascending order, or use a number-1 descending order. Options, optional parameters that represent the settings for indexing. The optional values are: Background,boolean, indexed in the background so that other database activities are not blocked when indexing is established. The default value is False. Unique,boolean, create a unique index. The default value is False. Name,string, specifies the name of the index. If not specified, MongoDB generates the name and sort order concatenation of an indexed field. Dropdups,boolean, when a unique index is created, only the first one is retained if duplicate deletions follow the same index. Sparse,boolean, does not enable indexing of field data that does not exist in the document. The default value is False. V,index version, the edition number of the index. Weights,document, the index weight value, which is between 1 and 99,999, indicates the score weight of the index relative to the other indexed fields.

For example, to create an index for a collection sites:

> Db.sites.ensureIndex ({name:1, Domain:-1})
{
  "createdcollectionautomatically": false,
  " Numindexesbefore ": 1,
  " Numindexesafter ": 2,
  " OK ": 1
}

Note: The index was created before version 1.8 using CreateIndex (), and the method has been removed after version 1.8


1.2 Reconstruction Index Reindex ()

Db. Collection_name.reindex ()

For example, rebuild all indexes for the collection sites:

> Db.sites.reIndex ()
{
  "Nindexeswas": 2,
  "Nindexes": 2, "
  indexes": [
    {"
	  key": {
		"_id  ': 1
	  },
	  ' name ': ' _id_ ',
		' ns ': ' Newdb.sites '
	},
	{
	  ' key ': {
		' name ': 1,
		' domain ': -1
	  },
	  "name": "Name_1_domain_-1",
	  "ns": "Newdb.sites"
	}
  ],
  "OK": 1
}


2. View Index

MongoDB provides a way to view index information: the Getindexes () method can be used to view all indexes of the collection, Totalindexsize () to view the total size of the collection index, Db.system.indexes.find () View all index information in the database.


2.1 View the indexes in the collection getindexes ()

Db. Collection_name.getindexes ()

For example, view the indexes in the collection sites:

>db.sites.getindexes ()
[
  {
	"V": 1,
	"key": {
	  "_id": 1
	},
	"name": "_id_",
	"ns": " Newdb.sites "
  },
  {
	" V ": 1,
	" key ": {
	  " name ": 1,
	  " domain ":-1
	},
	" name ":" Name_1 _domain_-1 ",
	ns:" Newdb.sites "
  }
]


2.2 View the index size in the collection totalindexsize ()

Db. Collection_name.totalindexsize ()

For example, view the collection sites index size:

> db.sites.totalIndexSize ()
16352


2.3 View all indexes in the database Db.system.indexes.find ()

Db.system.indexes.find ()

For example, all indexes for the current database:

> Db.system.indexes.find ()


3. Delete Index

No indexes are needed and we can delete them. When you delete an index, you can delete an index from the collection, and you can delete all indexes. 3.1 Delete the specified index Dropindex ()

Db. Collection_name.dropindex ("Index-name")

For example, delete the index named "Name_1_domain_-1" in the collection sites:

> Db.sites.dropIndex ("name_1_domain_-1")
{"Nindexeswas": 2, "OK": 1}


3.3 Delete all Indexes dropindexes ()

Db. Collection_name.dropindexes ()

For example, delete all indexes in the collection sites:

> db.sites.dropIndexes ()
{
  "Nindexeswas": 1,
  "msg": "non-_id indexes dropped for collection",
  "OK" : 1
}

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.