一:普通索引 1 创建一个新的数据库 use toto; switched to db toto show dbs; admin(empty) local0.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.
一:普通索引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();

4位age字段创建一个索引
db.c3.ensureIndex({age:1});
这时候可以看到服务器端有相应的输出
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();

【说明】:本文章由站长整理发布,文章内容不代表本站观点,如文中有侵权行为,请与本站客服联系(QQ:254677821)!