【问题标题】:Mongoose not returning full document猫鼬没有返回完整的文件
【发布时间】:2015-04-07 19:35:13
【问题描述】:

我使用 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 } 

此查询仅返回以下字段:名称、地址和 _id。

更新:

架构:

var locationSchema = mongoose.Schema({
    name: String,
    address: {city: String, street: String},
    location: {
        type: [Number],  // [<longitude>, <latitude>]
        index: '2dsphere'      // create the geospatial index
    }
});

【问题讨论】:

  • 你能发布你的架构定义吗?
  • 我更新了我的问题

标签: node.js mongodb mongoose


【解决方案1】:

架构中的 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 定义索引,因为它在架构中包含自己的字段。

【讨论】:

  • 谢谢您,先生。返回阅读有关旧坐标和新 GeoJSON 的更多信息。
猜你喜欢
  • 2016-11-14
  • 2018-07-12
  • 2020-07-21
  • 1970-01-01
  • 1970-01-01
  • 2021-05-25
  • 2015-08-05
  • 1970-01-01
  • 2015-10-08
相关资源
最近更新 更多