猫鼬不返回完整文件
Mongoose not returning full document
我使用 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 }
此查询仅return以下字段:名称、地址和_id。
更新:
架构:
var locationSchema = mongoose.Schema({
name: String,
address: {city: String, street: String},
location: {
type: [Number], // [<longitude>, <latitude>]
index: '2dsphere' // create the geospatial index
}
});
架构中的 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
定义索引,因为它在架构中包含自己的字段。
我使用 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 }
此查询仅return以下字段:名称、地址和_id。
更新:
架构:
var locationSchema = mongoose.Schema({
name: String,
address: {city: String, street: String},
location: {
type: [Number], // [<longitude>, <latitude>]
index: '2dsphere' // create the geospatial index
}
});
架构中的 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
定义索引,因为它在架构中包含自己的字段。