2016-09-24 51 views
1

我想用這個命令來使用不同的索引類型(2dsphere和文本)兩個指標:在MongoDB中一起創建地理空間和類型索引?

db.mycoll.createIndex({"@geolocationable":"2dsphere",name:"text"}) 

,但我得到以下錯誤:

"errmsg" : "bad index key pattern { @geolocationable: \"2dsphere\", name: \"text\" }: Can't use more than one index plugin for a single index."

我讀MongoDB Text and 2D compound index 但我我不確定爲什麼我不能在一個集合中創建2dsphere和文本索引。

我的意思不是說我想在一個查詢中使用這兩個指標的同時,我要創建這個索引,以便從他們在單獨的查詢單獨

回答

0

編輯使用:修改回答更新題。

如果兩個字段分別用於查詢,則可以創建2個不同的索引,而不是複合索引。

地理空間索引:

db.mycoll.createIndex({"@geolocationable":"2dsphere"}) 

文本索引:

db.mycoll.createIndex({name:"text"}) 

另外,從docs注意到

A collection can have at most one text index.

在創建複合索引,文本索引不能與多個或地理空間索引分組。這是一個複合指數限制。

docs

A compound text index cannot include any other special index types, such as multi-key or geospatial index fields.

不過,如果你不打算在name現場執行不區分大小寫的搜索,您可以創建一個正常的指標,而不是文本索引的複合索引。

db.mycoll.createIndex({"@geolocationable":"2dsphere",name:1}) 
+0

我已經更新了我不想在一個查詢中使用兩個索引的問題 – Mobin

+1

@Mobin:更新了我的答案 – 4J41

相關問題