Node.js Mongoose未返回完整文档
我使用mongolab作为我的数据库主机。 我创建了位置模式并确保索引“2dsphere” 文档示例:Node.js Mongoose未返回完整文档,node.js,mongodb,mongoose,Node.js,Mongodb,Mongoose,我使用mongolab作为我的数据库主机。 我创建了位置模式并确保索引“2dsphere” 文档示例: { "_id": { "$oid": "54d6347ce4b04aad9bbdc8ac" }, "name": "Vacation", "address": { "city": "Ashkelon", "street": "Afridat" }, "location": { "ty
{
"_id": {
"$oid": "54d6347ce4b04aad9bbdc8ac"
},
"name": "Vacation",
"address": {
"city": "Ashkelon",
"street": "Afridat"
},
"location": {
"type": "Point",
"coordinates": [
34.552955,
31.671384
]
}
}
当使用Mongoose查询集合时,一切都很好,但我无法检索位置字段。
使用MongoShell时,我会得到完整的文档(带有位置)
此查询仅返回以下字段:名称、地址和_id
更新:
模式:
var locationSchema = mongoose.Schema({
name: String,
address: {city: String, street: String},
location: {
type: [Number], // [<longitude>, <latitude>]
index: '2dsphere' // create the geospatial index
}
});
var locationSchema=mongoose.Schema({
名称:String,
地址:{城市:字符串,街道:字符串},
地点:{
类型:[编号],//[,]
索引:“2dsphere”//创建地理空间索引
}
});
架构中的位置
字段需要与文档中的结构匹配。您当前的模式将其定义为传统坐标对,但您的文档使用的是GeoJSON点
,因此请将您的模式改为如下所示:
var locationSchema = mongoose.Schema({
name: String,
address: {city: String, street: String},
location: {
type: { type: String },
coordinates: [Number]
}
});
locationSchema.index({location: '2dsphere'});
请注意,您需要单独定义
位置
的索引,因为它在架构中包含自己的字段。您可以发布架构定义吗?我更新了我的问题谢谢先生。返回阅读有关遗留坐标和新GeoJSON的更多信息。
var locationSchema = mongoose.Schema({
name: String,
address: {city: String, street: String},
location: {
type: { type: String },
coordinates: [Number]
}
});
locationSchema.index({location: '2dsphere'});