??
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