我有一个 mongoDB Schema ,如下所示:
var PlumberSchema = new Schema({
firstname: {
type: String
},
...
office: {
address:{ type:String },
location: { type:[Number], index: '2d'}
}
...
}
其中索引:' 2d' 是我需要为地理空间搜索创建2D索引的位置。
为了做到这一点,我已经阅读了文档:https://docs.mongodb.com/manual/tutorial/build-a-2d-index/并且有一个示例,为方便起见我报告:
db.<collection>.createIndex( {<location field> : "<index type>"}
如果我的架构是:,这可以帮助我
var PlumberSchema = new Schema({
firstname: {
type: String
},
...
address:{ type:String },
location: { type:[Number], index: '2d'}
...
}
要在这种情况下创建一个索引,我会写:
db.<collection>.createIndex( {location:"2d"})
但是你可以看到我有一个嵌套对象。我怎么能管理它?
答案 0 :(得分:0)
试试这个:
db.<collection>.createIndex( {"office.location":"2d"})