Indexes for MongoDB learning and sorting

Source: Internet
Author: User

The index information is stored in system. indexes.

Create an index:
DB. c1.ensureindex ({age: 1 },{ Background: true })
1: ascending-1: Descending
Background: whether to create an index in the background

Query the index of a table
DB. c1.getindexes () or db. system. Indexes. Find ()

Document index: the index will be created on fields of the embedded document type.
DB. Factories. insert ({name: "Baidu", ADDR: {City: "Beijing", state: "BJ "}})
DB. Factories. ensureindex ({ADDR: 1 })
DB. Factories. Find ({ADDR: {City: "Beijing", state: "BJ"}) -- Take the index
DB. Factories. Find ({ADDR: {state: "BJ", city: "Beijing"}) -- no index is taken and no data can be queried, because the order is inconsistent.

Combined index:
DB. Factories. ensureindex ({"ADDR. City": 1, "ADDR. State": 1 })
DB. Factories. Find ({"ADDR. City": "Beijing", "ADDR. State": "BJ"}) -- Take the index
DB. Factories. Find ({"ADDR. City": "Beijing"}) -- Take the index
DB. Factories. Find ({"ADDR. State": "BJ", "ADDR. City": "Beijing"}) -- do not go to the index, because the order is inconsistent

Unique index: ensureindex added to unique: True
DB. t4.ensureindex ({firstname: 1, lastname: 1 },{ unique: true })

Forcible indexing: using the hint command does not necessarily speed up indexing for all operations.
DB. t5.insert ({name: "liangzhangping", Age: 28}) -- add data for testing
DB. t5.ensureindex ({Name: 1, age: 1}) -- create an index
DB. t5.find ({age: {$ LT: 30}). Explain () -- check the execution plan to see if the index has been performed.

DB. t5.find ({age: {$ LT: 30}). Hint ({Name: 1, age: 1}). Explain ()

Delete an index and run the dropindex command.
1) Delete All indexes in the table, such as deleting all indexes in Table T3.
DB. t3.dropindexes ()
2) delete an index in a table. For example, delete the index created on the firstname field in Table T4:
DB. t4.dropindex ({firstname: 1 })

Re-indexing: Use the reindex command, for example, to re-create all indexes on table T3:
DB. t3.reindex ()

Optimizer: profiler, used to analyze the operation time for debugging and optimization. Information is saved in system. profile, and system. profile is a capped collection.

Start profiler:
1) Add "-profile = level" to start MongoDB"
/APP/Mongo/MongoDB/bin/mongod -- profile = 2
2) execute the DB. setprofilinglevel (level) command on the client, for example:
Primary> dB. setprofilinglevel (1)
Obtain the current level: DB. getprofilinglevel ()
Level:
0: Disabled
1: Record slow commands
2: record all commands

DB. setprofilinglevel (): records operations that exceed 10 ms



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.