Home > Database > Mysql Tutorial > 非关系型数据库(Nosql)之mongodb:普通索引,唯一索引

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

WBOY
Release: 2016-06-07 16:04:56
Original
1249 people have browsed it

一:普通索引 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

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

... }

>db.c3.count();

\

3无索引查找

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

\

4age字段创建一个索引

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

\

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template