문제 설명
스키마의 mongoose geojson, "지역 키를 추출할 수 없습니다" 오류 (mongoose geojson in schema, "Can't extract geo keys" error)
정의된 스키마가 있는 mongodb 컬렉션이 있으며 위도/경도 좌표를 포함하도록 이 스키마를 업데이트했습니다.
이전 버전:
var schema = mongoose.Schema({
id: String,
name: String,
address: String,
city: String,
zip: String,
country: String,
phoneNumber: String,
mobile: String,
website: String,
email: String,
});
새 버전
var schema = mongoose.Schema({
id: String,
name: String,
address: String,
city: String,
zip: String,
country: String,
phoneNumber: String,
mobile: String,
website: String,
email: String,
location: GeoJSON.Point
});
schema.index({ location: '2dsphere' });
GEOJSON.Point
는 mongoose‑geojson‑schema
에서 가져온 것으로 다음과 같습니다.
GeoJSON.Point = {
'type' : { type: String, default: "Point" },
coordinates: [
{type: "Number"}
]
}
컬렉션에는 location
속성.
분명히 지금 일어나는 일은 mongodb가 { 좌표: [], 유형: "Point" }
를 기본값으로 사용한다는 것입니다. 기존 문서에서 다음과 같은 오류가 발생합니다.
MongoError: Can't extract geo keys: { _id: ObjectId('5637ea3ca5b2613f37d826f6'), ...
Point must only contain numeric elements
스키마에서 기본값을 지정하는 방법을 살펴보았지만 값을 null
(GeoJSON.Point 데이터 유형의 경우).
또한 시도했습니다
db.collection.update({},{$set:{location:null},{multi:true})
하지만 그것도 도움이 되지 않는 것 같습니다.
위치
의 색인 때문인가요?
참조 솔루션
방법 1:
I think you need to upgrade GeoJSON.Point
to a sub document with a proper schema:
GeoJSON.Point = new mongoose.Schema({
'type' : { type: String, default: "Point" },
coordinates: [ { type: "Number" } ]
});
Combined with the minimize
option, which it enabled by default, this will make Mongoose only save the location
property if it is actually set.
방법 2:
I would suggest you to not use GeoJSON.Point
as a schema type. Just use mixed object type and everything will work properly.
location: Schema.Types.Mixed
(by Joris Mans、robertklep、Karlen)