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 ();