2013-06-27 74 views
0

我是MongoDB的新手,希望創建一個數據庫,其中包含存儲在集合中的位置數據(lat-long)。我計劃創建GeoJSON對象來存儲這些數據。 我想知道是否可以在這個集合上創建2個索引,其中一個是2Dsphere索引,另一個索引可以是任何其他值,如同一個集合中唯一的IP地址。 如果這是可能的,我該如何創建這些索引?使用2dsphere索引和IP地址索引對mongodb集合進行索引

+0

我創建了一個集合,並希望建立索引,但不知道如何做到這一點。 db.collection.ensureIndex( ....)將允許我在多個字段上創建索引,但我不知道如何創建具有2dsphere索引以及ipaddress索引的索引。 – koustubhC

回答

0

這應該工作:

db.position.ensureIndex({"geoField.coordinates":"2dsphere","ipaddress":1}) 

this article

看看我希望它能幫助

+0

謝謝安東尼奧 在這種情況下,文檔對象將如下權利?糾正我,如果我錯了 {geoField:{類型:點 座標:[緯度,經度]} ip地址:122.0.0.7 連接:無線網絡 } – koustubhC

+0

好了,如果你的文件是這樣的:'{」 geoField「:{」type「:」Point「,」coordinates「:[lat,long]},」ipaddress「:」122.0.0.7「,」connection「:」wi-fi「}'... then your statement創建索引將如下所示:'db.collection.ensureIndex({「geoField.coordinates」:「2dsphere」},{「ipaddress」:1})' – AntonioOtero

+0

如果ensureIndex格式不是:db.position.ensureIndex ({「geoField」:「2dsphere」,「ipaddress」:1}) – Alex