博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
mongodb索引
阅读量:2433 次
发布时间:2019-05-10

本文共 3756 字,大约阅读时间需要 12 分钟。

system.indexes下包含所有索引信息
自动创建的有每个集合的_id和shard key索引
mongos> db.system.indexes.find();
{ "v" : 1, "key" : { "_id" : 1 }, "name" : "_id_", "ns" : "test.doc1" }
{ "v" : 1, "key" : { "_id" : 1 }, "name" : "_id_", "ns" : "test.doc0" }
{ "v" : 1, "key" : { "_id" : 1 }, "name" : "_id_", "ns" : "test.doc2" }
{ "v" : 1, "key" : { "int1" : 1, "int2" : 1 }, "name" : "int1_1_int2_1", "ns" : "test.doc0" }
{ "v" : 1, "key" : { "datee" : 1 }, "name" : "datee_1", "ns" : "test.doc1" }
{ "v" : 1, "key" : { "_id" : "hashed" }, "name" : "_id_hashed", "ns" : "test.doc2" }
{ "v" : 1, "key" : { "int1" : 1 }, "name" : "int1_1", "ns" : "test.doc2" }
创建单列索引
mongos> db.doc2.createIndex({int1: 1});
{
     "raw" : {
          "rs0/rs0-1:4021,rs0-2:4022" : {
               "createdCollectionAutomatically" : false,
               "numIndexesBefore" : 2,
               "numIndexesAfter" : 3,
               "ok" : 1,
               "$gleStats" : {
                    "lastOpTime" : Timestamp(1427783921, 1),
                    "electionId" : ObjectId("551a2ae2990be7dcbb04bc36")
               }
          },
          "rs1/rs1-1:4031,rs1-2:4032" : {
               "createdCollectionAutomatically" : false,
               "numIndexesBefore" : 2,
               "numIndexesAfter" : 3,
               "ok" : 1,
               "$gleStats" : {
                    "lastOpTime" : Timestamp(1427783920, 1),
                    "electionId" : ObjectId("551a2ae0533749e2a0afdff5")
               }
          },
          "rs2/rs2-1:4041,rs2-2:4042" : {
               "createdCollectionAutomatically" : false,
               "numIndexesBefore" : 2,
               "numIndexesAfter" : 3,
               "ok" : 1,
               "$gleStats" : {
                    "lastOpTime" : Timestamp(1427783920, 1),
                    "electionId" : ObjectId("551a2ae17178e928a24df7d7")
               }
          },
          "rs3/rs3-1:4051,rs3-2:4052" : {
               "createdCollectionAutomatically" : false,
               "numIndexesBefore" : 2,
               "numIndexesAfter" : 3,
               "ok" : 1,
               "$gleStats" : {
                    "lastOpTime" : Timestamp(1427783921, 1),
                    "electionId" : ObjectId("551a2aec443124be35cfec75")
               }
          }
     },
     "ok" : 1
}
mongos> db.addr.insert(
... {
...  "name": "John Doe",
...  "
address": {
...         "street": "Main",
...         "
zipcode": "53511",
...         "state": "WI"
...         }
... });
WriteResult({ "nInserted" : 1 })
在内嵌对象上创建索引 
db.addr.createIndex( { "
address.zipcode": 1 } );
甚至直接为整个内嵌文档创建索引
db.addr.createIndex( {  
address: 1 });
复合索引
mongos> db.addr.insert(
...   {
...     id: 1,
...     name: "a"
...   }
... );
db.addr.createIndex({id:1, name:1});
复合索引的
限制
符合索引的某个键可以是数组
mongos> db.addr.insert(
...   {
...     id: [1,2],
...     name: "a"
...   }
... );
WriteResult({ "nInserted" : 1 })
mongos> db.addr.insert({
...     id: 1,
...     name: ["x","y"]
...   }
... );
WriteResult({ "nInserted" : 1 })
但是不能有两个键的值都是数组
mongos> db.addr.insert({
...     id: [1,2],
...     name: ["x","y"]
...   }
... );
WriteResult({
"nInserted" : 0,
"writeError" : {
"code" : 10088,
"errmsg" : "
cannot index parallel arrays [name] [id]"
}
})
multikey索引
mongos> db.addr.insert(
...   {
...     id: 1,
...     name: "a",
...     zips: [
...       {zipcode: 111},
...       {zipcode: 222},
...       {zipcode: 333}
...     ]
...   }
... );
上面addr的zips,是一个数组,里面有多个zipcode。可以用
db.addr.createIndex({'zips.zipcode': 1});
multikey索引的限制
shard key无法做multikey
哈希索引
和oracle一样,不支持范围扫描
db.aaa.createIndex( { a: "hashed" } )
TTL索引
数据插入一定时间后自动删除数据,单位是秒。
索引的字段必须是日期型,或包含日期类型的字段。
只支持单列索引。
db.doc2.createIndex({dt:1}, {expireAfterSeconds: 10})
db.doc2.insert({int1:99999, dt:new Date()})
...
...
上面设置的是10秒,但不一定真的能在10秒的时候删除。
sparse索引
这种索引只存储那些包含索引字段的文档,包括空值的。
db.doc2.createIndex( { "int2": 1 }, {  
sparse: true  } )
后台创建索引
默认情况下创建索引会阻塞数据库上所有操作,除非指定后台创建。
指定后台创建时,只有当前session会被阻塞。
进行后台创建时,无法进行其它与这个集合相关的管理类操作。
db.doc2.createIndex( { int3: 1}, {
background: true} )
删除索引
mongos> db.system.indexes.find({ns: "test.doc2"})
{ "v" : 1, "key" : { "_id" : 1 }, "name" : "_id_", "ns" : "test.doc2" }
{ "v" : 1, "key" : { "_id" : "hashed" }, "name" : "_id_hashed", "ns" : "test.doc2" }
{ "v" : 1, "key" : { "int1" : 1 },  
"name" : "int1_1", "ns" : "test.doc2" }
db.doc2.dropIndex("int1_1")

来自 “ ITPUB博客 ” ,链接:http://blog.itpub.net/26239116/viewspace-1485420/,如需转载,请注明出处,否则将追究法律责任。

转载于:http://blog.itpub.net/26239116/viewspace-1485420/

你可能感兴趣的文章
linux的signal_pending及signal
查看>>
OBJDUMP用法
查看>>
c/cplusplus通用makefile
查看>>
JavaScript-密码强度
查看>>
【SSH】1366-InCorrect string value:'\xE9\x99\x88\xE6\x96\xB0...'for column 'name' at row 1
查看>>
SpringCloud前身之微服务
查看>>
纵览全局——SSH
查看>>
纵览全局——Mybatis
查看>>
PC端-中文转拼音后续问题
查看>>
第七章-面向对象技术
查看>>
Mybatis-略识之无
查看>>
ionic 前端 - 汉字转拼音
查看>>
Ionic-与时间有关的故事-localecompare()
查看>>
Logback-spring.xml日志配置
查看>>
[Vue warn]: Property or method "name" is not defined on the instance but referenced during render
查看>>
ts:json串转换成数组
查看>>
String、StringBuffer和StringBuilder的区别
查看>>
java——职责链模式
查看>>
java_选择类排序——简单选择排序
查看>>
java_中介者模式
查看>>