我使用mongolab作为我的数据库主机。 我创建了位置模式并确保索引' 2dsphere' 文档示例:
{
"_id": {
"$oid": "54d6347ce4b04aad9bbdc8ac"
},
"name": "Vacation",
"address": {
"city": "Ashkelon",
"street": "Afridat"
},
"location": {
"type": "Point",
"coordinates": [
34.552955,
31.671384
]
}
}
使用Mongoose查询集合时,一切都很好,但我无法检索位置字段。 使用mongo shell时,我会获得完整的文档(包含位置)。
// the query Im using:
Mongoose: locations.find({ _id: ObjectId("54d63538e4b04aad9bbdc8b4") }) { fields: undefined }
此查询仅返回以下字段:name,address和_id。
更新
架构:
var locationSchema = mongoose.Schema({
name: String,
address: {city: String, street: String},
location: {
type: [Number], // [<longitude>, <latitude>]
index: '2dsphere' // create the geospatial index
}
});
答案 0 :(得分:0)
架构中的location
字段需要与文档中的结构相匹配。您当前的架构将其定义为旧版坐标对,但您的文档使用的是GeoJSON Point
,因此请将架构更改为以下内容:
var locationSchema = mongoose.Schema({
name: String,
address: {city: String, street: String},
location: {
type: { type: String },
coordinates: [Number]
}
});
locationSchema.index({location: '2dsphere'});
请注意,您需要单独定义location
的索引,因为它在架构中包含自己的字段。