mongodb指南(翻譯)(二十二) – developer zone – 索引(六)多鍵

來源:互聯網
上載者:User

Mongodb提供了一個有趣的“多鍵”特性,可以自動對對象的數組值進行索引。標籤就是個好例子。假定你有一篇包含了許多分類標籤的文章:

$ dbshell
> db.articles.save( { name: "Warm Weather", author: "Steve",
tags: ['weather', 'hot', 'record', 'april'] } )
> db.articles.find()
{"name" : "Warm Weather" , "author" : "Steve" ,
"tags" : ["weather","hot","record","april"] ,
"_id" : "497ce4051ca9ca6d3efca323"}

我們可以輕易的對tags數組中的值進行查詢:

> db.articles.find( { tags: 'april' } )
{"name" : "Warm Weather" , "author" : "Steve" ,
"tags" : ["weather","hot","record","april"] ,
"_id" : "497ce4051ca9ca6d3efca323"}

更進一步,我們可以對標籤數組進行索引。在數組上建立的索引,會對數組的每一個元素進行索引:

> db.articles.ensureIndex( { tags : 1 } )
true
> db.articles.find( { tags: 'april' } )
{"name" : "Warm Weather" , "author" : "Steve" ,
"tags" : ["weather","hot","record","april"] ,
"_id" : "497ce4051ca9ca6d3efca323"}
> db.articles.find( { tags: 'april' } ).explain()
{"cursor" : "BtreeCursor tags_1" , "startKey" : {"tags" : "april"} ,
"endKey" : {"tags" : "april"} , "nscanned" : 1 , "n" : 1 , "millis" : 0 }

增加和刪除關鍵字
你可以使用$addToSet增加一個新鍵到數組中,然後使用$pull移除該關鍵字。

> db.articles.update({name: "Warm Weather"},{$addToSet:{tags:"northeast"}});
> db.articles.find();
...
> db.articles.update({name: "Warm Weather"},{$pull:{tags:"northeast"}});

數組中的內嵌對象

同樣的技術還可以用來尋找內嵌在數組中的對象的欄位:

> // find posts where julie commented
> db.posts.find( { "comments.author" : "julie" } )
{"title" : "How the west was won",
"comments" : [{"text" : "great!" , "author" : "sam"},
{"text" : "ok" , "author" : "julie"}],
"_id" : "497ce79f1ca9ca6d3efca325"}

對給定集合中所有值進行查詢

使用$all選項,可以指定匹配所有給定集合中的值。例如:

> db.articles.find( { tags: { $all: [ 'april', 'record' ] } } )
{"name" : "Warm Weather" , "author" : "Steve" ,
"tags" : ["weather","hot","record","april"] ,
"_id" : "497ce4051ca9ca6d3efca323"}
> db.articles.find( { tags: { $all: [ 'april', 'june' ] } } )
> // no matches

對並行數組的警告

當使用複合索引時,至多可以包含一個數組關鍵字。所以,如果我們有一個索引{a:1,b:1},下面的文檔都是合法的:

{a: [1, 2], b: 1}
{a: 1, b: [1, 2]}

下面的文檔在插入時會失敗,並提示:“不能對並行數組建立索引”:

{a: [1, 2], b: [1, 2]}

該問題是由於複合鍵的笛卡爾積中的每一個值都要被索引,(如果支援並行數組的話,笛卡爾積會很大,導致索引非常大),這可能很快就會大到失控。

使用多鍵代替大量索引

一種查詢包含很多可選資料的方法是使用多鍵索引,此時的鍵都是對象。例如:

> x = {
... _id : "abc",
... cost : 33,
... attribs : [
... { color : 'red' },
... { shape : 'rect' },
... { color : 'blue' },
... { avail : true } ]
... };
> db.foo.insert(x);
> db.foo.ensureIndex({attribs:1});
> db.foo.find( { attribs : {color:'blue'} } ); // uses index
> db.foo.find( { attribs : {avail:false} } ); // uses index

除了可以包含不限數目的屬性類型,我們還能動態增加類型。

這在屬性查詢時很有用。上述模式對於排序或其他類型查詢並不是很有用。

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.