Introduction to MongoDB (3)--? The index of MongoDB

Source: Internet
Author: User
Tags create index mongodb touch

"MongoDB Entry" Reading notes download:
Index view index of http://download.csdn.net/detail/ch717828/9833847 MongoDB
Db.test_table.getIndexes () create INDEX (1 for ascending, 1 for descending)
Db.test_table.ensureIndex ({x:1})

Types of indexes

_ID Index

_id indexes are indexes that are established by default for most collections, and a unique _id field is automatically generated for each inserted data MongoDB

Single-Touch indexing

One-touch is the most common index

Db.test_table.ensureIndex ({x:1})

Multi-Key indexing

The value of a multi-key index has multiple records, such as an array, based on a single-button index

Db.test_table.insert ({x:[1,2,3,4,5]})

For this insert record, MongoDB creates a multi-key index for X

Composite Index

Use multiple keys as indexes

Db.test_table.ensureIndex ({x:1,y:1})

Expired Index

An index that expires after a certain period of time and the corresponding data is deleted after the index expires. This is ideal for storing data that will expire after a certain period of time, such as user login information, stored logs

Db.test_table.ensureIndex ({y:1},{expireafterseconds:30})
Db.test_table.insert ({y:new Date ()})
Db.test_ Table.insert ({y:1})

After observing a period of time, you will find that the value of Y is automatically deleted, and the value of Y is 1 is not deleted. So
The value stored in the Expired index field must be the specified time type (isodate or isodate array, cannot use timestamp). If a isodate array is established, it is deleted by the minimum time in the array. In addition, an expired index cannot be a composite index. Deletion time is not accurate (the removal process is timed by the daemon, and the deletion process takes time, so there is an error)

Full-Text Indexing

Create full-text indexes (only one full-text index is allowed per data collection)

Db.test_table.ensureIndex ({key: "text"}) 
Db.test_table.ensureIndex ({key_1: "text", Key_2: "Text"}) 
Db.test _table.ensureindex ({"$**": "Text"})  #$** represents the creation of a full-text index on all string fields.

Using Full-Text indexing

Db.test_table.insert ({"article": "ABCD abcd ABCD"}) 
Db.test_table.insert ({"article": "One by One"}) 
Db.test_ Table.ensureindex ({"article": "Text"})
Db.test_table.find ({$test: {$search: "ABCD"}})
Db.test_table.find ( {$test: {$search: "ABCD One"}}) 
Db.test_table.find ({$test: {search: "abcd-11"}}) # contains ABCD but does not contain one
Db.test_table.find ({$test: {$serach: "\" abcd\ "\" 11\ ""}) # includes ABCD and contains one
db.test_table.find ({$test: {$search: "ABCD 1234"}},{score:{$meta: "Textscore"}}) # Full-text index similarity

Location Index

Planar geo-Location index

Db.test_location.ensureIndex ({"W": "2d"}) # Creates a 2D planar geo-location index
db.test_location.insert ({w:[100,150]})   #插入记录
Db.test_location.find ({w:{$near: [First]}})  #查找距离 The nearest point (default 100)
Db.test_location.find ({w:{$ near:[1,1], $maxDistance: ten})  #查找距离 [10]
db.test_location.find ({w:{$geoWithin: {$box: [0,0],[ 3,3]]}}) #查找矩形 the point db.test_location.find in [[0,0],[3,3]]
({w:{$geoWithin: {$center: [[0,0],5]}}})  #查找圆心 [0,0] The point db.test_location.find within a circle with a radius of 5
({w:{$geoWithin: {$polygon: [[[0,0],[0,1],[2,2],[3,3]}}})  #查找多边形 [[0,0],[ 0,1],[2,2],[3,3]]
, point Db.runcommand ({geonear: "Test_location", near:[1,2], $maxDistance: 10,num:2}) #查找test_ In location, the maximum distance from [Max] is less than 10 of 2 records

Spherical geo-Location index

The name of the index
Db.test_table.ensureIndex ({x:1,y:1,z:1},{name: "normal}")

Uniqueness of the Index

Db.test_table.ensureIndex ({m:1,n:1},{unique:true})
Db.test_table.insert ({m:1,n:2})  #插入成功
Db.test_table.insert ({m:1,n:2})  #插入失败, Key violation

The sparsity of the index (a sparsity of TRUE indicates that no index is created for fields that do not exist)
Db.test_table.ensureIndex ({x:1},{sparse:true})

You cannot find records that do not exist on a sparse index, for example:

"'
Db.test_table.insert ({m:1})
Db.test_table.insert ({N:1})
Db.test_table.find ({m:{exists:true}}) #查找m存在的记录
Db.test_table.ensureIndex ({m:1},{sparse:true}) #创建稀疏索引
Db.test_table.find ({m:{exists:true}}) #查找m存在的记录 Db.test_table.ensureIndex ({m:1},{sparse:true}) #创建稀疏索引 Db.test_ Table.find ({M:{exists:false}}) #查找m不存在的记录, still finds M, which is a sparse index problem
Db.test_table.find ({m:{$exists: false}}). Hind ("m_1") #可以实现查找m不存在的记录

"' index build condition Analysis

Mongostat: Viewing the MongoDB running status program

./bin/mongostat--hlep   #查看mongostat帮助
./bin/mongostat-h 127.0.0.1:12345  #查看当前系统的运行情况 (see how many writes per second)

Explain: Displays the details of the query once

Db.test_table.find ({x:1}). Explain ();
MongoDB Security

Turn on MongoDB authentication
Vim conf/mongod.conf

Port = 12345
dbpath = data
LogPath = log/mongod.log
fork = true
Author = True
 "
Create a user using CreateUser

Db.createuser ({User: "TestUser", pwd: "TestUser", Roles:[{role: "Useradmin", DB: "admin"},{role: "read", DB: "Test"}]}) # Create TestUser user, have useradmin permission to admin, read permission to test
"' the role of multi-key indexing

Multi-key index and one-touch index in the use of a very different way, on the basis of a single-button index, if the inserted value is an array, MongoDB creates a multi-key index for it. When you query, you can find the record by using any of the values in the multi-key array.

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.