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 ();
watermark/2/text/ahr0cdovl2jsb2cuy3nkbi5uzxqvdg90b3r1envvcxvhbg==/font/5a6l5l2t/fontsize/400/fill/i0jbqkfcma== /dissolve/70/gravity/southeast ">
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 The corresponding value must be unique after a unique index has been established on the Join not go back and forth.
Db.c3.insert ({name: "Lisi", age:100});
watermark/2/text/ahr0cdovl2jsb2cuy3nkbi5uzxqvdg90b3r1envvcxvhbg==/font/5a6l5l2t/fontsize/400/fill/i0jbqkfcma== /dissolve/70/gravity/southeast ">
4 show Collections;
5 Db.system.indexes.find ();
4. Non-relational database (Nosql) MongoDB: Normal index, unique index