4.非关系型数据库(Nosql)之mongodb:普通索引,唯一索引



一:普通索引

1创建一个新的数据库

> use toto;

switched to db toto

> show dbs;

admin (empty)

local 0.078GB

> use toto;

switched to db toto

> db

toto

2创建100万条数据

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

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

... }

>db.c3.count();

3无索引查找

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

4age字段创建一个索引

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

这时候能够看到server端有对应的输出

 

5有索引查找

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

二:唯一索引

1删除索引:

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

2创建唯一索引

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

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

3在某个key上建立了唯一索引之后,这个相应的值必须唯一。加入不进去反复的了。

db.c3.insert({name:”lisi”,age:100});

4 show collections;

5 db.system.indexes.find();

 

原文地址:https://www.cnblogs.com/mfmdaoyou/p/6860391.html