mongodb索引

liiinuuux發表於2015-04-03
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/,如需轉載,請註明出處,否則將追究法律責任。

相關文章