mongodb指南(翻译)(二十二) - developer zone - 索引(六)多键
Mongodb提供了一個(gè)有趣的“多鍵”特性,可以自動(dòng)對(duì)對(duì)象的數(shù)組值進(jìn)行索引。標(biāo)簽就是個(gè)好例子。假定你有一篇包含了許多分類標(biāo)簽的文章:
$ 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"}
我們可以輕易的對(duì)tags數(shù)組中的值進(jìn)行查詢:
> db.articles.find( { tags: 'april' } ){"name" : "Warm Weather" , "author" : "Steve" ,
"tags" : ["weather","hot","record","april"] ,
"_id" : "497ce4051ca9ca6d3efca323"}
更進(jìn)一步,我們可以對(duì)標(biāo)簽數(shù)組進(jìn)行索引。在數(shù)組上創(chuàng)建的索引,會(huì)對(duì)數(shù)組的每一個(gè)元素進(jìn)行索引:
> 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 }
增加和刪除關(guān)鍵字
你可以使用$addToSet增加一個(gè)新鍵到數(shù)組中,然后使用$pull移除該關(guān)鍵字。
> db.articles.find();
...
> db.articles.update({name: "Warm Weather"},{$pull:{tags:"northeast"}});
數(shù)組中的內(nèi)嵌對(duì)象
同樣的技術(shù)還可以用來(lái)查找內(nèi)嵌在數(shù)組中的對(duì)象的字段:
> // 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"}
對(duì)給定集合中所有值進(jìn)行查詢
使用$all選項(xiàng),可以指定匹配所有給定集合中的值。例如:
> 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
對(duì)并行數(shù)組的警告
當(dāng)使用復(fù)合索引時(shí),至多可以包含一個(gè)數(shù)組關(guān)鍵字。所以,如果我們有一個(gè)索引{a:1,b:1},下面的文檔都是合法的:
{a: [1, 2], b: 1}{a: 1, b: [1, 2]}
下面的文檔在插入時(shí)會(huì)失敗,并提示:“不能對(duì)并行數(shù)組建立索引”:
{a: [1, 2], b: [1, 2]}該問題是由于復(fù)合鍵的笛卡爾積中的每一個(gè)值都要被索引,(如果支持并行數(shù)組的話,笛卡爾積會(huì)很大,導(dǎo)致索引非常大),這可能很快就會(huì)大到失控。
使用多鍵代替大量索引
一種查詢包含很多可選數(shù)據(jù)的方法是使用多鍵索引,此時(shí)的鍵都是對(duì)象。例如:
> 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
除了可以包含不限數(shù)目的屬性類型,我們還能動(dòng)態(tài)的增加類型。
這在屬性查詢時(shí)很有用。上述模式對(duì)于排序或其他類型查詢并不是很有用。
轉(zhuǎn)載于:https://www.cnblogs.com/xinghebuluo/archive/2012/01/31/2332766.html
總結(jié)
以上是生活随笔為你收集整理的mongodb指南(翻译)(二十二) - developer zone - 索引(六)多键的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: (收藏)Wp7开发中文网站
- 下一篇: jsp中有关于超链接的问题