MongoDB's Index Learning

Source: Internet
Author: User

To learn index classifications and create indexes:
1._ID Index The default index for most collections
2. Single key index: Manually created, a single value
3. Multi-built Index: composite function
4. Composite index: The leftmost prefix principle
5. Expired index: Expire within a certain time, note: must be isodate or its array, do not use timestamp, otherwise it will not be automatically deleted.
6. Full-text index Db.tm.ensureindex ({"article": "Text"}), Db.tm.ensureindex ({"Key1": "Text", "Key2": "Text"}), Db.tm.ensureindex ({$**: "text"})
Query: Db.tm.find ({$text: {$search: "AA"}})
Db.tm.find ({$text: {$search: "AA bb cc"}})
Db.tm.find ({$text: {$search: "AA Bb-cc"}})
Db.tm.find ({$text: {$search: "\" a\ "\" BB "\" Cc\ "}})
Full-text index matching degree $meta
Db.tm.find ({$text: {$search: "AA bb"}},{score:{$meta: "Textscore"}})
Db.tm.find ({$text: {$search: "AA bb"}},{score:{$meta: "Textscore"}}). sort ({score:{$meta: "Textscore"}})
Only $text can be specified at a time
7. Location index (drip taxi, Volkswagen reviews)


2d index (address location index)
Db.localtion.ensureindex ({"W": "2d"})

View index: Db.tm.getIndexes ()
Build Index Db.t1.ensureIndex ({x:1})
Multi-key index Db.tm.ensureIndex ({x:[1,2,3,4,5]})
Composite Index Db.tm.ensureIndex ({x:1,y:1})
Delete index Db.tm.dropIndex ("X_1_y_1")
Db.tm.find ({x:100,y:100}). Explain ()
Expiration index: Db.tm.insert ({time:new Date ()}) Isodate is the current time
Db.tm.ensureIndex ({time:1},{expireafterseconds:10})
Db.tm.insert ({time:new Date (), z:1})
Full-Text Indexing
Db.t1.ensureIndex ({article: "Text"})
Db.t1.insert ({article: "AA bb cc"})
Find Db.t1.find ({$text: {$search: "AA"}})
Db.t1.find ({$text: {$search: "AA bb cc"}}) or relationship
Db.t1.find ({$text: {$search: "AA Bb-cc"}}) does not contain CC
Db.t1.find ({$text: {$search: "\" aa\ "\" bb\ "\"-cc\ "}}) and the relationship


Similarity of full-text indexes: Db.t1.find ({$text: {$search: "AA bb"}},{score:{$meta: "Textscore"}})
Db.t1.find ({$text: {$search: "AA bb"}},{score:{$meta: "Textscore"}}). sort ({score:{$meta: "Textscore"}})



Index naming
Db.t1.ensureIndex ({x:1,y:2,z:2},{name: "xyz"})
Db.t1.dropIndex ("xyz")
Create a unique index
Db.t2.ensureIndex ({m:1,n:1},{unique:true})

View S index existence of a field

Db.abc.find ({m:{$exists: true}})

Create 2d Index: Planar location index, location representation, latitude [Longitude (-180,180), Latitude ( -90,90)]
Db.location.ensureIndex ({"W": "2d"})
Db.location.insert ({w:[1,1]})
Db.location.insert ({w:[1,2]})
Db.location.insert ({w:[3,2]})
Db.location.insert ({w:[32,22]})
Db.location.insert ({w:[100,90]})
Nearest query
Db.location.find ({w:{$near: [Up]}})

Inquire
Db.location.find ({w:{$near: [All], $maxDistance: 10}})

Address Location Index
Geonear
Db.runcommand ({geonear: "Location", near:[1,2],maxdistance:10,num:1})
Db.stats

for (i=1;i<10000;i++) Db.tt.insert ({n:i})

View Run status
/export/mongodb/bin/mongostat-h 192.168.1.70:22222
Faults locked IDX Miss does not use index values
QR|QW Read-write queue


View current level
Db.getprofilingstatus ()
0:profile is off, MongoDB does not log any operations
1 in conjunction with SLOWMS, MongoDB will record any operation over SLOWMS
2 will record any records
Modify Level Profile
Db.setprofilinglevel (0)
Db.system.profile.find (). Sort ({$natural: 1}). Limit (10)
Query sort

Db.system.indexes.find (). Sort ({$nature: 1})


This article is from the "DBSpace" blog, so be sure to keep this source http://dbspace.blog.51cto.com/6873717/1869918

MongoDB's Index Learning

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.