我在文档中有一个位置数组,我想在该数组上添加一个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 :(得分:6)
https://github.com/LearnBoost/mongoose/blob/master/examples/geospatial/geoJSONSchema.js#L19
LocationSchema.index({'geometry':'2dsphere'});
答案 1 :(得分:0)
如果您将位置的值存储为数组中的2个数字,那么您的索引将如下所示:
coordinates: { type: [Number], index: '2dsphere', required: true }
虽然你需要在数据库中创建这样的索引:
db.users.ensureIndex({ 'locations.geometry.coordinates': '2dsphere' });