保存位置 mongodb
Save location in mongodb
我正在尝试在我的 2dsphere
模型中保存坐标。
这是我的模型:
var userSchema = new Schema({
email: {
type: String,
required: true,
unique: true
},
...
loc: {
type: {
type: "String",
required: true,
enum: ['Point', 'LineString', 'Polygon'],
default: 'Point'
},
coordinates: [Number]
}
});
userSchema.index({'loc': '2dsphere'});
const User = mongoose.model('User', userSchema);
module.exports = User
我保存新数据的查询格式如下:
email:eu@vc.com
password:t12345
name:Igor
type:true
loc:{
coordinates: [-43.306174, -22.844279]
}
这是我收到的错误:
{
"code": 16804,
"index": 0,
"errmsg": "location object expected, location array not in correct format",
"op": {
"email": "eu@vc.com",
"password": "t12345",
"name": "Igor",
"_id": "5a311c016d15fc235895bef3",
"created_at": "2017-12-13T12:24:33.493Z",
"loc": {
"coordinates": [],
"type": "Point"
},
"type": true,
"__v": 0
}
}
您可能同时拥有 2d 和 2dsphere 索引。
下面的 error 表示您有一个二维索引。
"errmsg": "location object expected, location array not in correct format",
因此,当您尝试保存球坐标 ({"loc": {"coordinates": [23, 25],"type": "Point"}
) 时,您会在 mongodb 构建索引时遇到错误。
下面的错误 (first part & second part) 表明您有一个二维球体索引。
"errmsg": "Can't extract geo keys: {...loc: { coordinates: [23, 25] }} unknown GeoJSON type
因此,当您更改为 {"loc": {"coordinates": [23, 25] }}
时,二维球体索引无法构建。
您可以验证索引。 db.users.getIndexes()
删除 2d 索引(仍然存在是为了向后兼容)。 2d 球体索引同时支持传统坐标(例如 {"loc":[23, 25]}
)和地理坐标。
db.users.dropIndex( "loc_2d" )
这应该在 2d sphere index 重建索引以进行收集时起作用。
如果上述解决方案不起作用,请停止收集并重新开始。
db.users.drop()
你所有的文档'loc属性必须遵循geoJSON 格式。我看到一些文档在 loc[= 中有一个空数组 coordinates 26=] 属性 这是错误的。
"loc": {
"coordinates": [], //should NOT be an empty array, should be with 2 number items such as [ -43.306174, -22.844279]
"type": "Point"
},
确保一些格式错误的文件被清除。保持所有文件都遵循有效格式。
我正在尝试在我的 2dsphere
模型中保存坐标。
这是我的模型:
var userSchema = new Schema({
email: {
type: String,
required: true,
unique: true
},
...
loc: {
type: {
type: "String",
required: true,
enum: ['Point', 'LineString', 'Polygon'],
default: 'Point'
},
coordinates: [Number]
}
});
userSchema.index({'loc': '2dsphere'});
const User = mongoose.model('User', userSchema);
module.exports = User
我保存新数据的查询格式如下:
email:eu@vc.com
password:t12345
name:Igor
type:true
loc:{
coordinates: [-43.306174, -22.844279]
}
这是我收到的错误:
{
"code": 16804,
"index": 0,
"errmsg": "location object expected, location array not in correct format",
"op": {
"email": "eu@vc.com",
"password": "t12345",
"name": "Igor",
"_id": "5a311c016d15fc235895bef3",
"created_at": "2017-12-13T12:24:33.493Z",
"loc": {
"coordinates": [],
"type": "Point"
},
"type": true,
"__v": 0
}
}
您可能同时拥有 2d 和 2dsphere 索引。
下面的 error 表示您有一个二维索引。
"errmsg": "location object expected, location array not in correct format",
因此,当您尝试保存球坐标 ({"loc": {"coordinates": [23, 25],"type": "Point"}
) 时,您会在 mongodb 构建索引时遇到错误。
下面的错误 (first part & second part) 表明您有一个二维球体索引。
"errmsg": "Can't extract geo keys: {...loc: { coordinates: [23, 25] }} unknown GeoJSON type
因此,当您更改为 {"loc": {"coordinates": [23, 25] }}
时,二维球体索引无法构建。
您可以验证索引。 db.users.getIndexes()
删除 2d 索引(仍然存在是为了向后兼容)。 2d 球体索引同时支持传统坐标(例如 {"loc":[23, 25]}
)和地理坐标。
db.users.dropIndex( "loc_2d" )
这应该在 2d sphere index 重建索引以进行收集时起作用。
如果上述解决方案不起作用,请停止收集并重新开始。
db.users.drop()
你所有的文档'loc属性必须遵循geoJSON 格式。我看到一些文档在 loc[= 中有一个空数组 coordinates 26=] 属性 这是错误的。
"loc": { "coordinates": [], //should NOT be an empty array, should be with 2 number items such as [ -43.306174, -22.844279] "type": "Point" },
确保一些格式错误的文件被清除。保持所有文件都遵循有效格式。