2013-07-22 51 views
2

我有一個文檔中的位置數組,我想在該數組上添加一個2dSpere索引。那可能嗎?貓鼬,索引位置數組

var LocationSchema = new Schema({ 
    type: { type: String, required: true }, 
    geometry: { 
    type: { type: String, required: true }, 
    coordinates: { type: Array, required: true} 
    }, 
    properties: Schema.Types.Mixed 
}); 

// Collection to hold users 
var UserSchema = new Schema({ 
    username: { type: String, required: true, unique: true }, 
    locations: [LocationSchema] 
    },{ 
    versionKey: false 
    } 
); 

如何在locations數組中的幾何字段中添加2DSphere索引?

回答

0

如果您對位置值存儲在陣列2號,那麼你的指數看起來像:

coordinates: { type: [Number], index: '2dsphere', required: true } 

雖然你需要在數據庫中創建這樣的索引:

db.users.ensureIndex({ 'locations.geometry.coordinates': '2dsphere' });