mongodb-索引
说明:创建索引时,列名:int 中的int数字指的是正序或者倒序,如果是1表明是正序,-1表示倒序
1、查询collection上的索引
db.users.getIndexes()
2、查询当前的db的所有collection所拥有的索引
db.getCollectionNames().forEach(function(collection) {
indexes = db[collection].getIndexes();
print("Indexes for " + collection + ":");
printjson(indexes);
});
3、创建索引(background使创建索引的过程在后台完成,在索引创建期间可以处理其他请求,如果不加会阻塞创建索引期间的所有请求)
db.test.createIndex( {name: 1},{background:true} )
4、删除索引
db.test.dropIndex( {name :1} ) #删除name字段上的索引
db.test.dropIndexes() #删除collection下的所有索引除_id 这个主键索引
5、重建索引
db.test.reIndex( {name :1} )
6、Embedded Field创建索引
{ "_id" : ObjectId("57a1abd6369a01a77bb7f007"), "info" : { "age" : 32, "weight" : "75KG", "height" : "174cm" }, "contacts" : { "address" : "beijing", "phone" : 18651866297 } }
db.test.createIndex( { "contacts.phone" :1 },{ background:true} )
7、Embedded Field创建索引
{ "_id" : ObjectId("57a1abd6369a01a77bb7f007"), "info" : { "age" : 32, "weight" : "75KG", "height" : "174cm" }, "contacts" : { "address" : "beijing", "phone" : 18651866297 } }
db.test.createIndex( { "contacts.phone" :1 },{ background:true} )
8、查询执行计划
pandatv_msg:PRIMARY> db.test.find( { "contacts.phone": 18651866297} ).explain()
{
"queryPlanner" : {
"plannerVersion" : 1,
"namespace" : "gaoquan.test",
"indexFilterSet" : false,
"parsedQuery" : {
"contacts.phone" : {
"$eq" : 18651866297
}
},
"winningPlan" : {
"stage" : "FETCH",
"inputStage" : {
"stage" : "IXSCAN",
"keyPattern" : {
"contacts.phone" : 1
},
"indexName" : "contacts.phone_1",
"isMultiKey" : false,
"isUnique" : false,
"isSparse" : false,
"isPartial" : false,
"indexVersion" : 1,
"direction" : "forward",
"indexBounds" : {
"contacts.phone" : [
"[18651866297.0, 18651866297.0]"
]
}
}
},
"rejectedPlans" : [ ]
},
"serverInfo" : {
"host" : "mongo3v.notify.bjac.pdtv.it",
"port" : 27017,
"version" : "3.2.8",
"gitVersion" : "ed70e33130c977bda0024c125b56d159573dbaf0"
},
"ok" : 1
}
pandatv_msg:PRIMARY> db.test.find( { "contacts.address": "beijing"} ).explain()
{
"queryPlanner" : {
"plannerVersion" : 1,
"namespace" : "gaoquan.test",
"indexFilterSet" : false,
"parsedQuery" : {
"contacts.address" : {
"$eq" : "beijing"
}
},
"winningPlan" : {
"stage" : "COLLSCAN",
"filter" : {
"contacts.address" : {
"$eq" : "beijing"
}
},
"direction" : "forward"
},
"rejectedPlans" : [ ]
},
"serverInfo" : {
"host" : "mongo3v.notify.bjac.pdtv.it",
"port" : 27017,
"version" : "3.2.8",
"gitVersion" : "ed70e33130c977bda0024c125b56d159573dbaf0"
},
"ok" : 1
}
9、Embedded Document创建索引
{ "_id" : ObjectId("57a1abd6369a01a77bb7f007"), "info" : { "age" : 32, "weight" : "75KG", "height" : "174cm" }, "contacts" : { "address" : "beijing", "phone" : 18651866297 } }
db.test.createIndex( { "contacts.phone" :1 },{ background:true} )
10、创建组合索引
{ "_id" : ObjectId("57a1afe5369a01a77bb7f008"), "info" : { "age" : 32, "weight" : "75KG", "height" : "174cm" }, "contacts" : { "address" : "beijing", "phone" : 18651866297 }, "blog" : "http://home.cnblogs.com/u/gaoquan/", "company" : "pandatv" }
db.test.createIndex( { blog :1 , company: 1},{ background:true} )
11、创建前缀索引
{ "_id" : ObjectId("57a1afe5369a01a77bb7f008"), "info" : { "age" : 32, "weight" : "75KG", "height" : "174cm" }, "contacts" : { "address" : "beijing", "phone" : 18651866297 }, "blog" : "http://home.cnblogs.com/u/gaoquan/", "company" : "pandatv" }
db.test.createIndex( { blog :1 , company: 1},{ background:true} )
12、创建前缀索引
{ "_id" : ObjectId("57a1afe5369a01a77bb7f008"), "info" : { "age" : 32, "weight" : "75KG", "height" : "174cm" }, "contacts" : { "address" : "beijing", "phone" : 18651866297 }, "blog" : "http://home.cnblogs.com/u/gaoquan/", "company" : "pandatv", "hometown": "neimeng" }
db.test.createIndex( { blog :1 , company: 1, hometown: 1},{ background:true} )
说明:和关系型中的索引一样,组合索引中支持前缀索引,如上面索引,查询中包含blog,或者blog,company,或者blog,company,hometown都能使用到索引,如果是hometown则不能使用索引
13、创建多key索引
{ _id: 5, type: "food", item: "aaa", ratings: [ 5, 8, 9 ] }
{ _id: 6, type: "food", item: "bbb", ratings: [ 5, 9 ] }
{ _id: 7, type: "food", item: "ccc", ratings: [ 9, 5, 8 ] }
{ _id: 8, type: "food", item: "ddd", ratings: [ 9, 5 ] }
{ _id: 9, type: "food", item: "eee", ratings: [ 5, 9, 5 ] }
db.test.createIndex( { ratings: 1 } )
{ _id: 1, a: [ 1, 2 ], b: [ 1,2 ] ,category: "test "}这种类型的不能创建{ a: 1, b:1 }
14、统计索引大小
db.collection.totalIndexSize()
15、强制使用索引
cursor.hint()
db.users.find().hint( {age:1} )
db.users.find().max( { item: 'apple',type: 'jonagold' } ).hint( { item:1,type:1 })
16、查询最大值
cursor.hint()
db.users.find().hint( {age:1} )
mongodb-索引的更多相关文章
- [DataBase] MongoDB (7) MongoDB 索引
MongoDB 索引 1. 建立索引 唯一索引db.passport.ensureIndex( {"loginname": 1}, {"unique": tru ...
- MongoDB索引介绍
MongoDB中的索引其实类似于关系型数据库,都是为了提高查询和排序的效率的,并且实现原理也基本一致.由于集合中的键(字段)可以是普通数据类型,也可以是子文档.MongoDB可以在各种类型的键上创建索 ...
- MongoDB(索引及C#如何操作MongoDB)(转载)
MongoDB(索引及C如何操作MongoDB) 索引总概况 db.test.ensureIndex({"username":1})//创建索引 db.test.ensureInd ...
- MongoDB索引(一)
原文地址 一.介绍 我们已经很清楚索引会提高查询效率.如果没有索引,MongoDB必须对全部集合进行扫描,即,扫描集合中每条文档以选择那些符合查询条件的文档.对查询来说如果存在合适的索引,则Mongo ...
- MongoDB 索引篇
MongoDB 索引篇 索引的简介 索引可以加快查询的速度,但是过多的索引或者规范不好的索引也会影响到查询的速度.且添加索引之后的对文档的删除,修改会比以前速度慢.因为在进行修改的时候会对索引进行更新 ...
- MongoDB索引的种类与使用
一:索引的种类 1:_id索引:是绝大多数集合默认建立的索引,对于每个插入的数据,MongoDB都会自动生成一条唯一的_id字段2:单键索引: 1.单键索引是最普通的索引 2.与_id索引不同,单键索 ...
- MongoDB索引,性能分析
索引的限制: 索引名称不能超过128个字符 每个集合不能超过64个索引 复合索引不能超过31列 MongoDB 索引语法 db.collection.createIndex({ <field&g ...
- MongoDB索引原理
转自:http://www.mongoing.com/archives/2797 为什么需要索引? 当你抱怨MongoDB集合查询效率低的时候,可能你就需要考虑使用索引了,为了方便后续介绍,先科普下M ...
- MongoDB · 引擎特性 · MongoDB索引原理
MongoDB · 引擎特性 · MongoDB索引原理数据库内核月报原文链接 http://mysql.taobao.org/monthly/2018/09/06/ 为什么需要索引?当你抱怨Mong ...
- MongoDB索引管理
一.创建索引 创建索引使用db.collectionName.ensureIndex(...)方法进行创建: 语法: >db.COLLECTION_NAME.ensureIndex({KEY:1 ...
随机推荐
- iOS中如何隐藏启动图片的状态栏
只隐藏启动图片顶部的状态栏,而不影响程序运行起来控制器的状态栏显示?在info.plist文件中添加一个Statis bar is...... 默认是NO改为YES即可
- 【前端】event.target 和 event.currentTarget 的区别
event.target 和 event.currentTarget 的区别 举例说明: <!DOCTYPE html> <html> <head> <tit ...
- [issue] [iOS 10] 升级后无法真机测试 Could not find Developer Disk Image
说明:更新了手机的到了iOS 10.0.2.真机调试时候提示"Could not find Developer Disk Image" 并且之前就下载了Xcode8,但是没有安装X ...
- python操作Excel--使用xlrd
一.安装xlrd http://pypi.python.org/pypi/xlrd 二.使用介绍 1.导入模块 import xlrd 2.打开Excel文件读取数据 data = xlrd.open ...
- mysql 存储过程和事件调度
存储过程(procedure): 建立一个存储过程需要知道的基础知识 1.确定输入/输出的参数和类型: IN tname varchar(20) 其中 IN 表示输入参数,tname 是参数名 va ...
- BW顾问必需要清楚的:时间相关数据建模场景需求分析
声明:原创作品,转载时请注明文章来自SAP师太技术博客( 博/客/园www.cnblogs.com):www.cnblogs.com/jiangzhengjun,并以超链接形式标明文章原始出处,否则将 ...
- 用FileInputStream读取数据,计算机如何实现将两个字节拼接成中文的?
package itcast_02; import java.util.Arrays; /* * 在计算机中如何识别将连个字节转换为中文的呢? * 在计算机中中文的存储为两个字节 : * 第一个字节 ...
- java学习笔记----枚举测试题
定义义一个交通灯枚举类,包含红灯.绿灯.黄灯,需要有获得下一个灯的方法,并实现红灯出现5秒之后变成绿灯,绿灯3秒之后变成黄灯,黄灯2秒之后变成红灯,如此循环 public class Test5 { ...
- Babel 学习
一,为了更明白地使用Babel, 先了解Babel 的发展过程. 现在Babel的版本是6, 相对于以前的版本, 它做了重大更新: 1, 模块化:所有的内部组件都变成了单独的包.打开Babel在Git ...
- (十七)linux网络命令 vconfig ifconfig
增删VLAN vconfig add eth0 10 vconfig rem eth0.10重启网卡 ifconfig eth0.101 up ifconfig eth0.10 ...