Mongodb for non-relational databases (Nosql): Common Index, unique index

Source: Internet
Author: User
I. Create a new database usetoto; switchedtodbtotoshowdbs; admin (empty) local0.078GBusetoto; alias creates 1 million pieces of data for (vari1; i000000; I ++ ){... db. c3.insert ({name: zhangsan, age: I });...} db.

I. Create a new database use toto; switched to db toto show dbs; admin (empty) local0.078GB use toto; switched to db toto 2 create 1 million pieces of data for (var I = 1; I = 1000000; I ++ ){... db. c3.insert ({name: zhangsan, age: I });...} db.

I. Normal Indexes

1. Create a new database

> Use toto;

Switched to db toto

> Show dbs;

Admin (empty)

Local 0.078 GB

> Use toto;

Switched to db toto

> Db

Toto

>

2. Create 1 million data entries

> For (var I = 1; I <= 1000000; I ++ ){

... Db. c3.insert ({name: "zhangsan", age: I });

...}

> Db. c3.count ();

3 No index search

> Db. c3.find ({age: 500000}). explain ();

Create an index for the 4-digit age Field

Db. c3.ensureIndex ({age: 1 });

At this time, we can see that the server has the corresponding output

5. Search with indexes

Db. c3.find ({age: 500000}). explain ();

Ii. Unique Index

1. delete an index:

Db. c3.dropIndex ({age: 1 });

2. Create a unique index

Db. c3.ensureIndex ({age: 1 },{ unique: true });

Db. c3.find ({age: 500000}). explain ();

3. After a unique index is created on a key, the corresponding value must be unique and cannot be added.

Db. c3.insert ({name: "lisi", age: 100 });

4 show collections;

5 db. system. indexes. find ();

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.