4. Non-relational database (Nosql) MongoDB: Normal index, unique index

Source: Internet
Author: User

??

One: Normal index

1 to create a new database

> Use Toto;

Switched to DB Toto

> Show DBS;

Admin (empty)

Local 0.078GB

> Use Toto;

Switched to DB Toto

> DB

Toto

>

2 Create - million-piece data

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

... db.c3.insert ({name: "Zhangsan", age:i});

... }

>db.c3.count ();

3 No index lookup

>db.c3.find ({age:500000}). Explain ();

4 bit Age field to create an index

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

You can see the server side has the corresponding output

5 have index lookup

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

Two: Unique index

1 To 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 in a Key after a unique index has been established, the corresponding value must be unique and not be added repeatedly.

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

4 show Collections;

5 Db.system.indexes.find ();

4. Non-relational database (Nosql) MongoDB: Normal index, unique 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.