【问题标题】:Mongoose query doesn´t return geometry property of GeoJSONMongoose 查询不返回 GeoJSON 的几何属性
【发布时间】:2018-12-02 11:02:52
【问题描述】:

鉴于我的数据库中的以下文档:

{
    "_id" : ObjectId("5c03b36d13032cc192d33f84"),
    "type" : "Feature",
    "properties" : {
        "name" : "point 1",
        "amenity" : "test",
        "popupContent" : "test"
    },
    "geometry" : {
        "type" : "Point",
        "coordinates" : [ 
           [ -3.68814468383789, 
            40.5248912033234]
        ]
    }
}

并给出以下猫鼬查询:

function findVertex(req,res){
    Vertex2.find({"_id":"5c03b36d13032cc192d33f84"}, function(err,obj) {
        if(err){
            res.status(500).send(error);
        }else{
            var geoJson=obj[0];

            console.log(geoJson)
            res.status(200).send(geoJson);
        }

    })
}

我收到的是:

{ properties: { name: 'point 1', amenity: 'test', popupContent: 'test' },
  _id: 5c03b36d13032cc192d33f84,
  type: 'Feature' }

为什么响应中没有几何?如果我在 robomongo 中执行相同的查询,我会得到带有坐标的对象。

【问题讨论】:

  • 你的 mongoose 和 mongodb 版本是什么?

标签: node.js mongoose leaflet gis geojson


【解决方案1】:

mongoDb GeoJSON object documnet 您的point 几何结构的基数不正确,coordinates 必须是包含 2 个元素的数组,而不是包含另一个数组的数组

【讨论】:

  • 那是真的,我犯了一个错误,我应该输入多点,因为我想向特征添加更多点,所以它应该是数组到数组中,无论如何,它没有不工作
【解决方案2】:

最后我修复了它更改模型架构。

最初我有这个:

 _id:String,
type:String,
properties:{
    name:String,
    amenity:String,
    popupContent:String
},
geometry:{
    type:String,
    coordinates:[]
}

现在我有了这个:

type:String,
    properties:{
        name:String,
        amenity:String,
        popupContent:String
    },
    geometry:{
        type: {type: String, default: 'MultiPoint'},
        coordinates: {type: []}
    }  

我得到了想要的对象作为回报:

{"properties":{"name":"Coors Field","amenity":"Baseball Stadium","popupContent":"This is where the Rockies play!"},"geometry":{"type":"MultiPoint","coordinates":[[-3.68814468383789,40.5248912033234],[-3.70814468383789,40.5248912033234],[-3.68230819702148,40.5074040815359]]},"_id":"5c03a3da82822133f0406667","type":"Feature","__v":0}

【讨论】:

    猜你喜欢
    • 2021-07-22
    • 2022-06-14
    • 2014-10-09
    • 1970-01-01
    • 2014-12-04
    • 1970-01-01
    • 2019-08-29
    • 2018-06-20
    • 1970-01-01
    相关资源
    最近更新 更多