mongodb索引
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" }
{ "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
}
{
"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 })
... {
... "name": "John Doe",
... "address": {
... "street": "Main",
... "zipcode": "53511",
... "state": "WI"
... }
... });
WriteResult({ "nInserted" : 1 })
在內嵌物件上建立索引
db.addr.createIndex( { "address.zipcode": 1 } );
db.addr.createIndex( { "address.zipcode": 1 } );
甚至直接為整個內嵌文件建立索引
db.addr.createIndex( { address: 1 });
複合索引
mongos> db.addr.insert(
... {
... id: 1,
... name: "a"
... }
... );
... {
... 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}
... ]
... }
... );
... {
... 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" }
{ "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/,如需轉載,請註明出處,否則將追究法律責任。
相關文章
- MongoDB 索引MongoDB索引
- mongodb索引使用MongoDB索引
- mongoDB的索引MongoDB索引
- MongoDB索引概述MongoDB索引
- MongoDB之索引(地理資訊索引)MongoDB索引
- MongoDB之索引(全文索引)MongoDB索引
- MongoDB之索引(過期索引)MongoDB索引
- MongoDB之索引(唯一索引)MongoDB索引
- MongoDB索引,效能分析MongoDB索引
- mongodb建立索引和刪除索引和背景索引backgroundMongoDB索引
- mongodb 如何檢視索引MongoDB索引
- MongoDB ( 五 )高階_索引MongoDB索引
- MongoDB之索引(簡介)MongoDB索引
- MongoDB索引實戰技巧MongoDB索引
- 快速掌握mongoDB(三)——mongoDB的索引詳解MongoDB索引
- 【MongoDB學習筆記】MongoDB索引那點事MongoDB筆記索引
- MongoDB系列一(索引及C#如何操作MongoDB)MongoDB索引C#
- MongoDB索引優化詳解MongoDB索引優化
- MongoDB中的定時索引MongoDB索引
- 005.MongoDB索引及聚合MongoDB索引
- MongoDB索引的簡單理解MongoDB索引
- MongoDB慢查詢與索引MongoDB索引
- MongoDB複合索引詳解MongoDB索引
- 【Mongo】MongoDB索引管理-索引的建立、檢視、刪除MongoDB索引
- MongoDB索引與優化詳解MongoDB索引優化
- mongodb資料庫如何建立索引?MongoDB資料庫索引
- MongoDB中複合索引結構MongoDB索引
- MongoDB如何在後臺建立索引MongoDB索引
- MongoDB技巧——不要到處使用索引MongoDB索引
- MongoDB學習之豐富的索引MongoDB索引
- mongodb索引及查詢優化分析MongoDB索引優化
- mongodb 重複建立索引不報錯MongoDB索引
- MongoDB範圍查詢的索引優化MongoDB索引優化
- 聊聊非關係型資料庫MongoDB索引資料庫MongoDB索引
- Mongodb在replicaset的secondary上補建索引MongoDB索引
- MongoDB正規表示式在索引中的使用MongoDB索引
- 「生產事故」MongoDB複合索引引發的災難MongoDB索引
- 分散式文件儲存資料庫之MongoDB索引管理分散式資料庫MongoDB索引