MongoDB notes (ii) Index

Source: Internet
Author: User
Tags createindex

Version: mongodb3.4;

Index :

If MongoDB cannot sort using an index, the data is placed in memory for sorting, and when memory usage exceeds 32MB, an error is made.

When you create an index, you should ensure that the index is selected to avoid unnecessary queries. Avoid indexes that do not have a selective force.

_ID is the default index.

Basic methods:

Db.collection.createIndex ({a:1});

Db.collection.dropIndex ({a:1}); Both methods are removed.

. Dropindex (' IndexName ');

Db.collection.dropIndexes ();

Db.collection.getIndexes ();

Db.collection.reIndex (); Rebuild Index

  

  one ,single indexes index:

{Field:1}

  two ,Compound Indexes composite Index:

{A:1,b:1}

Two field are indexed.

Query: Find ({a:value})//use index;

Find ({a:value, b:value})//use index;

Find ({}). sort ({b:1}) //nonuse; When a composite index is used alone, only the first (a) is recognized. The index intersection is used here to succeed.

Find ({}). sort ({a:1})//use

. Sort ({a:1})//use;

. Sort ({a:1,b:1})//use;

. Sort ({a:-1,b:-1})//use;

. Sort ({a:-1,b:1}) //nonuse;

  three ,Multikey indexes Multi-key index:

1. :

Document: {A: [All-in-all]}

Index: {a:1}

Find ({a:1})//Use this index so;

2. Composite Index:

Only one multi-key index can appear in a composite index, so:

Index:{a:1,b:1}

Insert ({a:[1,2],b:[1,2]})//error;

Insert ({a:[1,2], b:1})//pass;

3.the array Field contain nested Objects:

Document: {A: [{a:1, b:2}, {a:3, b:4}]},

index{A.a:1} By doing so, the index is established;

four , Textindex:

Matching documents by regular means is very expensive. So there is such a text Index;

Use: CreateIndex ({A: ' text '}), which is used by default in English.

The language can be defined by a second attribute: CreateIndex ({A: ' text '}, {default_language: ' Hans '});

Hans is simplified Chinese. Unfortunately, Mongod Enterprise Edition can use the text Index in Chinese.

five ,index intersection the intersection of indexes:

   For example, you have the following index:

Index:{a:1},

Index:{b:1,c:1},

Index {b:1}

Find ({a:value, b:value})//Support

Find ({A:value}). Sort ({b:1})//Not supported

Find ({B:value}). Sort ({c:1})//support, using {b:1,c:1} This index, although there is {b:1} this single index;

  six ,Index Properties indexed property:

    1, TTL(set document expiration time):

Db.test.createIndex ({expire:1}, {expireafterseconds:3000});

A expire index is recommended, and the value it accepts in document must be: Bson date type. or array<bson>;.

The inserted document will be deleted after its specified time +3000s.

That is, when you insert, you get the expiration time. MongoDB periodically compares this expiration time to the current time, which is less than or equal to the current time when the data expires. Like what:

Db.test.insert ({expire:new Date ()}); Will expire after 3000S.

The following example is followed:

 

var time= new Date (); Time.setfullyear (Time.getfullyear ()-1);d b.test.update ({....},{expire:time});                      Change the time to earlier so that document expires prematurely; Db.test.update ({....},{$currentDate: {expire:true}})       //fix time to current, forcibly renew!

The expire in MONGDB is not the same as in Redis. Need more understanding.

2, uniquesingle index :

Create ({id:1}, {unique:ture})

The ID of each document in collection is unique and the duplicate cannot write.

3,partical Local index :

Create ({name:1}, {age:{$gt: 20}});

Only document with age greater than 20 is indexed. When in conjunction with unique, the index area is not subject to unique restrictions.

4,sparse sparse Index :

Create ({name:1},{sparse:true});

When not in use, db.test.distinct (' name ') returns a null when one or more of the document does not have a name attribute. These documents are traversed because of the query.

Use sparse, db.test.distinct (' name '). Document without the name attribute is not queried. Returns the due value.

Db.find ({}). sort ({name:1})//Does not use sparse indexes by default. So instead of forcing the specified index here:

Db.find ({}). sort ({name:1}). Hint ({name:1})//forcibly use name Index;

Db.find ({}). Hint ({$natural: 1})//Disable all indexes for querying

To create an index on Replset:

    https://docs.mongodb.com/manual/tutorial/build-indexes-on-replica-sets/

MongoDB notes (ii) Index

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.