mongodb指南(翻譯)(十八) – developer zone – 索引(二)_id索引、複合索引、稀疏索引、數組索引、唯一索引

來源:互聯網
上載者:User

_id索引

除了定容量集合外的所有集合,都會自動為_id欄位建立一個索引。這是一個特殊索引並且不能被刪除。該_id索引強制它的關鍵字都是唯一的(除了分區環境下的一些情景)。

_id值是恒定不變的。

對內嵌關鍵字進行索引(“點標記法”)

在Mongodb中,你可以對內嵌文檔的關鍵字進行索引。訪問子文檔的方法被視為點標記法。例如:

db.things.ensureIndex({"address.city": 1})

文檔作為關鍵字

被索引的欄位可以是任意類型,包括(內嵌的)文檔:

db.factories.insert( { name: "xyz", metro: { city: "New York", state: "NY" } } );
db.factories.ensureIndex( { metro : 1 } );
// this query can use the above index:
db.factories.find( { metro: { city: "New York", state: "NY" } } );
// this one too, as {city:"New York"} < {city:"New York",state:"NY"}
db.factories.find( { metro: { $gte : { city: "New York" } } } );
// this query does not match the document because the order of fields is significant
db.factories.find( { metro: { state: "NY" , city: "New York" } } );

將文檔作為關鍵字的替代方法是建立一個複合式索引:

db.factories.ensureIndex( { "metro.city" : 1, "metro.state" : 1 } );
// these queries can use the above index:
db.factories.find( { "metro.city" : "New York", "metro.state" : "NY" } );
db.factories.find( { "metro.city" : "New York" } );
db.factories.find().sort( { "metro.city" : 1, "metro.state" : 1 } );
db.factories.find().sort( { "metro.city" : 1 } )

這兩種方法各有優缺點。當使用整個(子)文檔作為一個關鍵字,比較順序是預定義的並且是以關鍵字在BSON文檔出現的升序來排序的。在複合式索引中,你可以混合升序和降序的關鍵字,查詢最佳化工具還是可以只使用索引中的第一個關鍵字進行查詢。

複合式索引

除了基本的單個關鍵字索引外,Mongodb還支援多建“組合”索引。正如基本索引那樣,你可以在shell中使用ensureIndex()來建立複合式索引,但不像基本索引僅可以指定一個關鍵字,此時你可以指定多個:

db.things.ensureIndex({j:1, name:-1});

當建立一個索引時,關鍵字後面的數字指定了索引排序的方向,因此它只能是1(升序)或者-1(降序)。對於單鍵索引或者隨機訪問來說方向並不重要,但是當你對複合式索引做排序或者範圍查詢時,方向就很重要了。

如果你有一個建立在多欄位上的複合式索引,你可以使用它查詢多鍵前面的子集欄位。因此如果你有一個索引在:

a,b,c

你可以使用它對這些欄位進行查詢:

a

a,b

a,b,c

對數組內元素進行索引

如果文檔中要建立索引的欄位是是一個數組,Mongodb會為數組中每一個元素建立索引。請移步多建章節查看詳細內容。

 

稀疏索引

“稀疏索引”就是僅包含有被索引欄位的文檔的索引。

任何沒有稀疏索引欄位的文檔都不會被儲存到該索引中;這種索引就是由於不包含沒有索引欄位的文檔而被稱為稀疏索引的。

根據定義,稀疏索引並不完整(對該集合而言)並且與完整索引表現不同。當使用“稀疏索引”進行排序(或者是過濾),集合中的一些文檔可能不會返回。這是因為只有該集合中的文檔會返回。

> db.people.ensureIndex({title : 1}, {sparse : true})
> db.people.save({name:"Jim"})
> db.people.save({name:"Sarah", title:"Princess"})
> db.people.find()
{ "_id" : ObjectId("4de6abd5da558a49fc5eef29"), "name" : "Jim" }
{ "_id" : ObjectId("4de6abdbda558a49fc5eef2a"), "name" : "Sarah", "title" : "Princess" }
> db.people.find().sort({title:1}) // only 1 doc returned because sparse
{ "_id" : ObjectId("4de6abdbda558a49fc5eef2a"), "name" : "Sarah", "title" : "Princess" }
> db.people.dropIndex({title : 1})
{ "nIndexesWas" : 2, "ok" : 1 }
> db.people.find().sort({title:1}) // no more index, returns all documents
{ "_id" : ObjectId("4de6abd5da558a49fc5eef29"), "name" : "Jim" }
{ "_id" : ObjectId("4de6abdbda558a49fc5eef2a"), "name" : "Sarah", "title" : "Princess" }

你可以將稀疏和唯一聯合起來建立唯一約束從而忽略掉沒有對應欄位的文檔。

注意Mongodb的稀疏索引不是阻塞型索引。Mongodb稀疏索引可以被認為是指定過濾器的稠密索引。
唯一索引

Mongodb支援唯一索引,它保證跟已有文檔的索引關鍵字重複的文檔不會被插入。建立保證不會有兩個文檔在欄位firstname和lastname上有重複值,你可以這樣做:

db.things.ensureIndex({firstname: 1, lastname: 1}, {unique: true});

唯一索引和不存在的關鍵字

當儲存到集合中的文檔在索引欄位沒有值的話,它的索引欄位會被賦值為null然後插入。就是說,你不可能在唯一索引中插入多個在某個索引欄位都沒有值的文檔。

db.things.ensureIndex({firstname: 1}, {unique: true});
db.things.save({lastname: "Smith"});
// Next operation will fail because of the unique index on firstname.
db.things.save({lastname: "Jones"});

dropDups

如果在某個欄位已經存在重複值,那麼將不能在該欄位建立唯一索引。如果無論如何你都要建立唯一索引,並且僅保留資料庫中在該欄位第一個出現的文檔然後刪除所有在該欄位有重複值的文檔,請增加“dropDups”選項:

db.things.ensureIndex({firstname : 1}, {unique : true, dropDups : true})

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.