[英]MongoDB Malformed Geometry with geojson
使用MongoDB v2.6.5
當我嘗試將geojson文檔保存到數據庫中時,出現以下錯誤:
name: 'MongoError',
code: 16755,
err: 'insertDocument :: caused by :: 16755 Can\'t extract geo keys from object, malformed geometry?: { _id: ObjectId(\'55271b90d075728028d4c9e1\'), ..., location: [ { _id: ObjectId(\'55271b90d075728028d4c9e3\'), loc: { type: "Point", coordinates: [ -105.01621, 39.57422 ] } } ] } ], status: [ "lead" ], created: new Date(1428626320406), lastName: "Doe", firstName: "John", __v: 0 }' }
我試圖將Point插入具有2dsphere索引的表中,全部通過MongooseJS進行管理,如下所示。
var GeoAddressSchema = new Schema({
// Only holds points.
loc: {
type: { type: String },
coordinates: []
}
});
var Lead = new Schema({
// Other fields ...
location: [GeoAddressSchema],
// ...
});
LeadAddressSchema.index({ location: '2dsphere' });
正在保存的geojson:
{ type: "Point", coordinates: [ -111.855211, 33.58513 ] }
如果我將字段用引號引起來,則geojson根據以下網址有效: http ://geojsonlint.com/,但我不需要這樣做(並且對於Mongo afaik來說也不能)。
有人知道為什么會失敗嗎? 看起來很正確。
參考鏈接
MongoDB GeoJSON: http : //docs.mongodb.org/manual/reference/geojson/
MongoDB 2dSphere: http ://docs.mongodb.org/manual/core/2dsphere/
初步觀察:您無需在位置結構中引入loc
路徑。
此外,您甚至不需要為Lead
模式中的location
字段定義單獨的模式。
嘗試這個:
var Lead = new Schema({
// Other fields ...
location: {
type: {
type: 'String',
default: 'Point' // and optionally skip including `type: String` everywhere
},
coordinates: {
type: [Number]
}
},
// More fields ...
});
LeadAddressSchema.index({ location: '2dsphere' });
我面臨的另一個問題是,在確定和測試解決方案時2dsphere
索引會混亂。 因此,在對架構進行結構更改后,請嘗試刪除索引,甚至刪除更好的集合。
> db.lead.drop(); // on the mongo console`
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.