MongoDB development and learning (2) basic indexing operations

Source: Internet
Author: User
Tags mongodb server

Indexes can greatly improve the query efficiency. Resume indexes are essential in the database.

You can easily create indexes in MongoDB.

 

Default index _ id _

Enable the MongoDB server, create the database cnblogs, and create the set users. (For this part of knowledge, please refer to the previous article on MongoDB development and learning (1) getting started with apsaradb for MongoDB)

In the shell window, type the following command to query all indexes in the Set:

 
> DB. Users. getindexes ()

From the output, we can see that the system has created an index _ id _ for the users set by default _.

MongoDB has a default "_ id" field "in each set, which is equivalent to" primary key ". After the set is created, the system automatically creates an index on the "_ id" key, which is the default index. The index name is "_ id _" and cannot be deleted.

 

Create an index

Use the ensureindex () command in MongoDB to create an index

1) create a singleton Index

 
> DB. Users. ensureindex ({Name: 1 })

2) create a joint index

A joint index is to use multiple fields as an index.

> DB. Users. ensureindex ({Name: 1, sex:-1}) //The number following the keyword indicates the sorting direction of the index, 1 indicates the ascending order, and-1 indicates the reverse order. //The default index name format is "keyword _ number ",For example, the two indexes created above are named "name_1" and "name_policsex _-1"

3) create a document index

The so-called document index means that a "field" in the set is a document, and then an index is created for this field.

You can create an index for the entire "field", for example:

 
> DB. Users. ensureindex ({'Article': 1 })

You can also index the resume of a certain attribute of the "field" document, for example:

 
> DB. Users. ensureindex ({'Article. title': 1 })

 

Delete Index

Use the dropindex () command in MongoDB to delete indexes, for example:

 
> DB. Users. dropindex ('name _ 1 ')

Related reading:MongoDB development and learning (1) getting started with a classic

 

Address: http://www.cnblogs.com/xumingxiang/archive/2012/04/09/2438939.html

Author: Xu mingxiang
Source: http://www.cnblogs.com/xumingxiang
Copyright: The copyright of this article is shared by the author and the blog
Reprinted: you are welcome to reprinted. To save the author's Creative Enthusiasm, please [reprinted] As required. Thank you.
Requirement: This statement must be retained without the author's consent; Article The original text connection is provided; otherwise, the legal liability is required.

 

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.