如何使用 mongodb 保存 geojson?
how to save geojson with mongodb?
我正在尝试使用 mongoose 和 nodejs 保存一个 geojson,目前我想保存一个 'MultiPoint'
类型的 geojson
这是我定义的保存geojson的方案
let capaSchema = new Schema({
nombrecapa: {
type: String,
required: [true, 'El nombre de la capa es necesario']
},
descripcion: {
type: String,
required: [false]
},
geojson: Object([
geoSchema
])
});
const geoSchema = new Schema({
type: {
type: String,
default: 'FeatureCollection',
},
features: [
Object({
type: {
type: String,
default: 'Feature',
},
geometry: {
type: {
type: String,
default: 'MultiPoint'
},
coordinates: {
type: [
Number
],
index: '2dsphere'
}
}
})
],
});
这是我要用moongose的save方法保存的对象,首先我做了一个schema的实例,也许我的错误可能在实例内部。
let capa = new Capa({
nombrecapa: body.nombrecapa,
descripcion: body.descripcion,
geojson: {
type: body.typefeature,
features: [{
type: body.featurestype,
geometry: {
type: body.geometrytype,
coordinates: [
[-105.01621, 39.57422],
[-105.01231, 39.57321]
]
}
}
]
}
});
capa.save((err, capadb) => {
if (err) {
return res.status(400).json({
ok: false,
err
})
}
res.json({
ok: true,
capa: capadb
})
})
但在保存时我返回了以下错误:
"_message": "Capa validation failed",
"message": "Capa validation failed: geojson.0.features.0.geometry.coordinates: Cast to Array failed for value \"[ [ -105.01621, 39.57422 ], [ -105.01231, 39.57321 ] ]\" at path \"geometry.coordinates\"",
"name": "ValidationError"
在您的架构中,您将 coordinates
作为单个数组,但传递的数据实际上是嵌套数组的数组
我认为你需要的是
coordinates: {
type: [[Number]],
index: '2dsphere'
}
我正在尝试使用 mongoose 和 nodejs 保存一个 geojson,目前我想保存一个 'MultiPoint'
类型的 geojson这是我定义的保存geojson的方案
let capaSchema = new Schema({
nombrecapa: {
type: String,
required: [true, 'El nombre de la capa es necesario']
},
descripcion: {
type: String,
required: [false]
},
geojson: Object([
geoSchema
])
});
const geoSchema = new Schema({
type: {
type: String,
default: 'FeatureCollection',
},
features: [
Object({
type: {
type: String,
default: 'Feature',
},
geometry: {
type: {
type: String,
default: 'MultiPoint'
},
coordinates: {
type: [
Number
],
index: '2dsphere'
}
}
})
],
});
这是我要用moongose的save方法保存的对象,首先我做了一个schema的实例,也许我的错误可能在实例内部。
let capa = new Capa({
nombrecapa: body.nombrecapa,
descripcion: body.descripcion,
geojson: {
type: body.typefeature,
features: [{
type: body.featurestype,
geometry: {
type: body.geometrytype,
coordinates: [
[-105.01621, 39.57422],
[-105.01231, 39.57321]
]
}
}
]
}
});
capa.save((err, capadb) => {
if (err) {
return res.status(400).json({
ok: false,
err
})
}
res.json({
ok: true,
capa: capadb
})
})
但在保存时我返回了以下错误:
"_message": "Capa validation failed",
"message": "Capa validation failed: geojson.0.features.0.geometry.coordinates: Cast to Array failed for value \"[ [ -105.01621, 39.57422 ], [ -105.01231, 39.57321 ] ]\" at path \"geometry.coordinates\"",
"name": "ValidationError"
在您的架构中,您将 coordinates
作为单个数组,但传递的数据实际上是嵌套数组的数组
我认为你需要的是
coordinates: {
type: [[Number]],
index: '2dsphere'
}