--單索引git
> db.test.ensureIndex({"username":1})spring
複合索引mongodb
--數字1表示username鍵的索引按升序存儲,-1表示age鍵的索引按照降序方式存儲。json
>db.test.ensureIndex({"username":1, "age":-1}) bash
爲索引建立名字工具
> db.test.ensureIndex({"username":1},{"name":"testindex"}) post
爲內嵌文檔創建索引值spa
>db.test.ensureIndex({"comments.date":1})3d
> db.test.ensureIndex({"userid":1},{"unique":true})code
--建立惟一索引,並消除重複數據。
> db.test.ensureIndex({"userid":1},{"unique":true,"dropDups":true})
> db.test.dropIndex({"userid":1})
explain是很是有用的工具,會幫助你得到查詢方面諸多有用的信息。只要對遊標調用該方法,就能夠獲得查詢細節。explain會返回一個文檔,而不是遊標自己。如:
> db.test.find().explain("executionStats")
explain會返回查詢使用的索引狀況,耗時和掃描文檔數的統計信息。
最初的文本搜索只是一種試驗性功能,但從 2.6 版本起就成爲默認功能了。但若是使用的是以前的 MongoDB,則須要使用下列代碼啓用文本搜索:
>db.adminCommand({setParameter:true,textSearchEnabled:true})
假設在 posts 集合中的下列文檔中含有帖子文本及其標籤。
{ "post_text": "enjoy the mongodb articles on tutorialspoint", "tags": [ "mongodb", "tutorialspoint" ] }
咱們將在 post_text 字段上建立文本索引,以便搜索帖子文本以內的內容。
>db.posts.ensureIndex({post_text:"text"})
如今咱們已經在 post_text 字段上建立了文本索引,接下來搜索包含 tutorialspoint 文本內容的帖子。
>db.posts.find({$text:{$search:"tutorialspoint"}})
在返回的結果文檔中,果真包含了具備 tutorialspoint 文本的帖子文本。
{ "_id" : ObjectId("53493d14d852429c10000002"), "post_text" : "enjoy the mongodb articles on tutorialspoint", "tags" : [ "mongodb", "tutorialspoint" ] } { "_id" : ObjectId("53493d1fd852429c10000003"), "post_text" : "writing tutorials on mongodb", "tags" : [ "mongodb", "tutorial" ] }
若是用的是舊版本的 MongoDB,則需使用下列代碼:
>db.posts.runCommand("text",{search:" tutorialspoint "})
總之,與普通搜索相比,使用文本搜索可極大地改善搜索效率。
spring-data-mongodb使用方法
http://spring.io/blog/2014/07/17/text-search-your-documents-with-spring-data-mongodb
MongoDB的地理位置查詢
MongoDB地理位置索引經常使用的有兩種。
創建索引:
> db.places.ensureIndex({'coordinate':'2d'}) > db.places.ensureIndex({'coordinate':'2dsphere'})
查詢方式:
而查詢座標參數則分兩種:
1.座標對(經緯度)根據查詢命令的不一樣,$maxDistance距離單位多是 弧度 和 平面單位(經緯度的「度」):
db.<collection>.find( { <location field> : { $nearSphere: [ <x> , <y> ] , $maxDistance: <distance in radians> } } )
2.GeoJson $maxDistance距離單位默認爲米:
db.<collection>.find( { <location field> : { $nearSphere : { $geometry : { type : "Point" , coordinates : [ <longitude> , <latitude> ] } , $maxDistance : <distance in meters> } } } )
查詢當前座標附近的目標,由近到遠排列。
能夠經過$near或$nearSphere,這兩個方法相似,但默認狀況下所用到的索引和距離單位不一樣。
查詢方式:
> db.places.find({'coordinate':{$near: [121.4905, 31.2646]}}) > db.places.find({'coordinate':{$nearSphere: [121.4905, 31.2646]}})
查詢結果:
{ "_id" : 115, "coordinate" : { "longitude" : 121.4915, "latitude" : 31.25933 }, "title" : "僅售148元,市場價298元的星程上服假日酒店全日房一間入住一天, 節假日通用,精緻生活,品質享受", "address" : "虹口區天水路90號" } // …(100條)
上述查詢座標[121.4905, 31.2646]附近的100個點,從最近到最遠排序。
默認返回100條數據,也能夠用limit()指定結果數量,如:
db.places.find({'coordinate':{$near: [121.4905, 31.2646]}}).limit(2)
指定最大距離 $maxDistance
> db.places.find({'coordinate':{$near: [121.4905, 31.2646], $maxDistance:2}})
MongoDB中的範圍搜索(Inclusion)主要用$geoWithin這個命令,它又細分爲3種不一樣類型,以下:
(A) 矩形查詢:
> db.places.find( { coordinate : { $geoWithin : { $box :[ [ 121.44, 31.25 ] , [ 121.5005, 31.2846 ] ] } } } )
(B) 圓形查詢
應用場景有:地圖搜索租房信息
查詢以某座標爲圓心,指定半徑的圓內的數據。
前面已提到,圓形區域搜索分爲$center和$centerSphere這兩種類型,它們的區別主要在於支持的索引和默認距離單位不一樣。2d索引能同時支持$center和$centerSphere,2dsphere索引支持$centerSphere。關於距離單位,$center默認是度,$centerSphere默認距離是弧度。
> db.places.find({'coordinate':{$geoWithin:{$centerSphere:[ [121.4905, 31.2646] , 0.6/111] }}})
db.places.find({'coordinate':{$geoWithin:{$centerSphere:[ [121.4905, 31.2646] , 0.6/6371] }}})
查詢結果:
{ "_id" : 115, "coordinate" : { "longitude" : 121.4915, "latitude" : 31.25933 }, "title" : "僅售148元,市場價298元的星程上服假日酒店全日房一間入住一天,節假日通用, 精緻生活,品質享受", "address" : "虹口區天水路90號" } ...
(C) 多邊形查詢
複雜區域內的查詢,這個應用場景比較少見。指定至少3個座標點,查詢方式以下(五邊形):
> db.places.find( { coordinate : { $geoWithin : { $polygon : [ [121.45183 , 31.243816] , [121.533181, 31.24344] , [121.535049, 31.208983] , [121.448955, 31.214913] , [121.440619, 31.228748] ] } } } )
假設須要以當前座標爲原點,查詢附近指定範圍內的餐廳,並直接顯示距離。
這個需求用前面提到的$near是能夠實現的,可是距離須要二次計算。這裏咱們用$geoNear這個命令查詢。
$geoNear與$near功能相似,但提供更多功能和返回更多信息,官方文檔是這麼解釋的
$near方法查詢後會對結果集對距離進行排序,而$geoWithin是無序的
> db.runCommand( { geoNear: "places", near: [ 121.4905, 31.2646 ], spherical: true, maxDistance:1/6371, num:2 }) { "ns" : "mongo_test.places", "near" : "1110001100111100001011010110010111001000110011111101", "results" : [ { "dis" : 0.00009318095248858048, "obj" : { "_id" : 115, "coordinate" : { "longitude" : 121.4915, "latitude" : 31.25933 }, "title" : "僅售148元,市場價298元的星程上服假日酒店全日房一間入住一天, 節假日通用,精緻生活,品質享受", "address" : "虹口區天水路90號" } }, { "dis" : 0.00010610660597329082, "obj" : { "_id" : 465, "coordinate" : { "longitude" : 121.48406, "latitude" : 31.26202 }, "title" : "【四川北路】熱烈慶祝康駿會館成立8週年!僅售169元!市場價399元的 康駿會館四川北路一店(僅限3星級技師)全身精油按摩一人次!全程約90分鐘! 男女不限!僅限四川北路一店使用,非本市全部門店通用!拉手券消費僅限每日19:00前! 健康有道,駿越萬里!", "address" : "虹口區四川北路1896號-1904號201室" } } ], "stats" : { "time" : 0, "btreelocs" : 0, "nscanned" : 18, "objectsLoaded" : 12, "avgDistance" : 0.00009964377923093564, "maxDistance" : 0.0001064199324957278 }, "ok" : 1 }