MONGODB(二)——索引操作
2015-12-31 13:49
543 查看
一、
1、插入10w条数据
> for(var i = 0;i<100000;i++){
... var rand = parseInt(i*Math.random());
... db.person.insert({"name":"hxc"+i,"age":i})
... }
2、使用性能分析函数explain
> db.person.find({“name”:”hxc”+1000}).explain()
{
“cursor” : “BasicCursor”, //表扫描,也就是顺序查找
“isMultiKey” : false,
“n” : 1, //返回了一个文档
“nscannedObjects” : 100006, //浏览的文档数
“nscanned” : 100006,
“millis” : 58, //总共耗时58毫秒
}
3、建立索引
> db.person.ensureIndex({“name”:1})
> db.person.find({“name”:”hxc”+1000}).explain()
{
“cursor” : “BtreeCursor name_1″,采用B树结构存储索引,索引名为”name_1″
“isMultiKey” : false,
“n” : 1,
“nscannedObjects” : 1,
“nscanned” : 1, //!!!!
“nscannedObjectsAllPlans” : 1,
“nscannedAllPlans” : 1,
“nChunkSkips” : 0,
“millis” : 0, //!!!!
}
二、
1、唯一索引
建立唯一索引,重复键值不能输入。唯一索引也不能再已经存在重复键值的表上创建
db.person.ensureIndex({“name”:1},{“unique”:true})。
2、组合索引
> db.person.ensureIndex({“name”:1,”birthday”:1})
> db.person.ensureIndex({“birthday”:1,”name”:1})
查看生成的索引> db.person.getIndexes()
{
“v” : 1,
“key” : {
“name” : 1,
“birthday” : 1
},
“ns” : “test.person”,
“name” : “name_1_birthday_1″
},
{
“v” : 1,
“key” : {
“birthday” : 1,
“name” : 1
},
“ns” : “test.person”,
“name” : “birthday_1_name_1″
}
3、删除索引
> db.person.dropIndexes(“name_1″)
{
“nIndexesWas” : 4,
“msg” : “non-_id indexes dropped for collection”,
“ok” : 1
}
1、插入10w条数据
> for(var i = 0;i<100000;i++){
... var rand = parseInt(i*Math.random());
... db.person.insert({"name":"hxc"+i,"age":i})
... }
2、使用性能分析函数explain
> db.person.find({“name”:”hxc”+1000}).explain()
{
“cursor” : “BasicCursor”, //表扫描,也就是顺序查找
“isMultiKey” : false,
“n” : 1, //返回了一个文档
“nscannedObjects” : 100006, //浏览的文档数
“nscanned” : 100006,
“millis” : 58, //总共耗时58毫秒
}
3、建立索引
> db.person.ensureIndex({“name”:1})
> db.person.find({“name”:”hxc”+1000}).explain()
{
“cursor” : “BtreeCursor name_1″,采用B树结构存储索引,索引名为”name_1″
“isMultiKey” : false,
“n” : 1,
“nscannedObjects” : 1,
“nscanned” : 1, //!!!!
“nscannedObjectsAllPlans” : 1,
“nscannedAllPlans” : 1,
“nChunkSkips” : 0,
“millis” : 0, //!!!!
}
二、
1、唯一索引
建立唯一索引,重复键值不能输入。唯一索引也不能再已经存在重复键值的表上创建
db.person.ensureIndex({“name”:1},{“unique”:true})。
2、组合索引
> db.person.ensureIndex({“name”:1,”birthday”:1})
> db.person.ensureIndex({“birthday”:1,”name”:1})
查看生成的索引> db.person.getIndexes()
{
“v” : 1,
“key” : {
“name” : 1,
“birthday” : 1
},
“ns” : “test.person”,
“name” : “name_1_birthday_1″
},
{
“v” : 1,
“key” : {
“birthday” : 1,
“name” : 1
},
“ns” : “test.person”,
“name” : “birthday_1_name_1″
}
3、删除索引
> db.person.dropIndexes(“name_1″)
{
“nIndexesWas” : 4,
“msg” : “non-_id indexes dropped for collection”,
“ok” : 1
}
参考
[1] 《8天学通MongoDB》 http://www.cnblogs.com/huangxincheng/archive/2012/02/18/2356595.html相关文章推荐
- MongoDB(一)
- Spark 连接mysql 及MongoDB
- MongoDB:Map-Reduce
- apache log4j将日志保存在mongodb数据库中
- mongodb的备份和还原
- Mongodb管理的几个常识
- MongoDB常用命令及例子详细介绍
- MongoDB GridFS
- mongoDB 入门指南、示例
- Java操作MongoDB
- Mongodb数据导出工具mongoexport和导入工具mongoimport介绍
- mongodb安装遇到的一个坑!!(mongodb 32位版本 win7 )
- 安装mongodb配置windows服务流程
- mongodb 安装使用遇到的问题记录
- java实现mongodb的数据库连接池
- MongoDB 驱动以及分布式集群读取优先级设置
- MongoDB学习笔记六:进阶指南
- Mongodb 笔记02 创建、更新和删除文档
- MongoDB学习笔记五:聚合
- C# 对MongoDB 进行增删改查的简单操作