我正在尝试将一个坐标对推入一个文档中,该文档包含嵌套如下的GeoJSON MultiLineString:[[[Number]]]
当我这样找到OneAndUpdate时:
要求的身体点 = [-123.347,48.43649]
Route.findOneAndUpdate({name: req.body.name},
{'$push': { "route.coordinates.0": req.body.point}}, {new: true})
.then(doc => {
return res.send(doc)
}).catch(err => {
res.send(err)
})
我收到错误消息:
errmsg": "Can't extract geo keys: { _id: ObjectId('5be9eef393311d2d2c6130fd').......
Point must only contain numeric elements",
"code": 16755,
我的坐标是mongo期望的有效[long,lat]格式。 这里有我想念的东西吗?
这是我的MultiLineString架构:
const MultiLineStringSchema = new Schema({
type: {
type: String,
enum: ['MultiLineString'],
required: true,
default: 'MultiLineString'
},
coordinates: {
type: [[[Number]]],
required: true
}
});
这是我的路线图:
var { MultiLineStringSchema } = require('./GeoJson-Schemas')
const RouteSchema = new mongoose.Schema({
name: String,
route: {
type: MultiLineStringSchema,
required: true
}
});
RouteSchema.index({ route: "2dsphere" });
编辑2: 这是保存该错误的路线文档。我已使用此文档重新创建了错误,并更新了上面错误消息中的匹配_id。
{
"_id": {
"$oid": "5be9eef393311d2d2c6130fd"
},
"name": "A Route",
"route": {
"type": "MultiLineString",
"coordinates": [
[
[
-123.3867645,
48.4813423
],
[
-123.3785248,
48.4592626
],
[
-123.3766365,
48.4527165
],
[
-123.3756924,
48.4523749
],
[
-123.3722591,
48.4549366
],
[
-123.3704567,
48.4559612
]
]
],
"_id": {
"$oid": "5be9eef393311d2d2c6130fe"
}
},
"__v": 0
}
向前迈出了一步,我向MultiLineString子文档中添加了{_id:false}选项,并重新初始化了一个新集合。像这样存储新文档后:
{
"_id": "5be9f076e1caaa23682d80de",
"name": "A Route",
"route": {
"type": "MultiLineString",
"coordinates": [
[
[
-123.3867645,
48.4813423
],
[
-123.3785248,
48.4592626
],
[
-123.3766365,
48.4527165
],
[
-123.3756924,
48.4523749
],
[
-123.3722591,
48.4549366
],
[
-123.3704567,
48.4559612
]
]
]
},
"__v": 0
}
我尝试使用确切的语法来查找OneAndUpdate:
Route.findOneAndUpdate({name},
{'$push': { "route.coordinates.0": point}}, {new: true})
.then(doc => {
return res.send(doc)
}).catch(err => {
res.send(err)
})
并收到相同的错误:
"errmsg": "Can't extract geo keys: { _id: ObjectId('5be9f076e1caaa23682d80de')...
Point must only contain numeric elements",
"code": 16755,
编辑3:
再次前进,我完全更新了RouteSchema 删除 子文档,就像这样:
const RouteSchema = new mongoose.Schema({
name: String,
route: {
type: {
type: String,
enum: ['MultiLineString'],
required: true,
default: 'MultiLineString'
},
coordinates: {
type: [[[Number]]],
required: true
}
}
});
我这样存储文档:
{
"_id": {
"$oid": "5be9f3915fc64e3548603766"
},
"route": {
"type": "MultiLineString",
"coordinates": [
[
[
-123.3867645,
48.4813423
],
[
-123.3785248,
48.4592626
],
[
-123.3766365,
48.4527165
],
[
-123.3756924,
48.4523749
],
[
-123.3722591,
48.4549366
],
[
-123.3704567,
48.4559612
]
]
]
},
"name": "A Route",
"__v": 0
}
和FindOneAndUpdate再次使用确切的语法,并收到相同的错误。
Route.findOneAndUpdate({name},
{'$push': { "route.coordinates.0": point}}, {new: true})
.then(doc => {
return res.send(doc)
}).catch(err => {
res.send(err)
})
"errmsg": "Can't extract geo keys: { _id: ObjectId('5be9f3915fc64e3548603766')
Point must only contain numeric elements",
"code": 16755,
编辑4:
mLab hosted instance mongo version 3.6.6 indexes on Route collection PHOTO
在外壳程序连接上运行db.routes.getIndexes()可提供以下内容。
[
{
"v" : 2,
"key" : {
"_id" : 1
},
"name" : "_id_",
"ns" : "testbench.routes"
},
{
"v" : 2,
"key" : {
"route" : "2dsphere"
},
"name" : "route_2dsphere",
"ns" : "testbench.routes",
"background" : true,
"2dsphereIndexVersion" : 3
}
]
答案 0 :(得分:0)
因此,该问题确实是可重现的,并且可以归结为架构定义。就在这里:
coordinates: {
type: [[[Number]]],
required: true
}
在这里,您努力地指定文档MultiLineString
格式所需的嵌套数组“ rings”。看起来似乎正确,插入新文档不是问题,但是请注意MongoDB想要在这里使用的符号:
{ $push: { 'route.coordinates.0': [-123.3701134, 48.4467389] } }
因此,“模式”具有一个[[[Number]]]
中的“双数组嵌套”,但是MongoDB只是希望coordinates.0
而不是coordinates.0.0
才能感到高兴。这是猫鼬困惑的地方,并且“重写” 更新语句:
{ '$push': { 'route.coordinates.0': [ [ -123.3701134 ], [ 48.4467389 ] ] } }
所以这是错误的,并且是错误的根源:
errmsg: '无法提取地理位置键:{_id:ObjectId(\'5be9f3915fc64e3548603766 \'),路线:{类型:“ MultiLineString”,坐标:[[[-123.3867645,48.4813423],[-123.3785248、48.4592626],[- 123.3766365,48.4527165],[-123.3756924、48.4523749],[-123.3722591、48.4549366],[-123.3704567、48.4559612],[[-123.3701134],[48.4467389]]]]},名称:“一条路线”,__v:0 }点只能包含数字元素”,
将架构更改为“非特定”可阻止猫鼬“破坏”该语句:
coordinates: {
type: [] // Just expect an array without any other definition
required: true
}
然后更新将按预期进行。
有关完整的可复制清单:
const { Schema, Types: { ObjectId } } = mongoose = require('mongoose');
// Sample connection
const uri = 'mongodb://localhost:27017/geostring';
const opts = { useNewUrlParser: true };
// Sensible defaults
mongoose.Promise = global.Promise;
mongoose.set('useFindAndModify', false);
mongoose.set('useCreateIndex', true);
mongoose.set('debug', true);
// Schema defs
const routeSchema = new Schema({
name: String,
route: {
type: {
type: String,
enum: ['MultiLineString'],
required: true,
default: 'MultiLineString'
},
coordinates: {
type: [],
// type: [[[Number]]], // this has a problem
required: true
}
}
});
routeSchema.index({ route: '2dsphere' });
const Route = mongoose.model('Route', routeSchema);
// log helper
const log = data => console.log(JSON.stringify(data, undefined, 2));
// Main
(async function() {
try {
const conn = await mongoose.connect(uri, opts);
// clean data from all models
await Promise.all(
Object.entries(conn.models).map(([k,m]) => m.deleteMany())
);
// Insert data
await Route.create({
"_id": new ObjectId("5be9f3915fc64e3548603766"),
"name": "A Route",
"route": {
"type": "MultiLineString",
"coordinates": [
[
[
-123.3867645,
48.4813423
],
[
-123.3785248,
48.4592626
],
[
-123.3766365,
48.4527165
],
[
-123.3756924,
48.4523749
],
[
-123.3722591,
48.4549366
],
[
-123.3704567,
48.4559612
]
]
]
}
});
// Test operation
let result = await Route.findOneAndUpdate(
{ name: 'A Route' },
{ $push: { 'route.coordinates.0': [-123.3701134, 48.4467389] } },
{ new: true }
);
log(result);
} catch(e) {
console.error(e);
} finally {
mongoose.disconnect();
}
})()
以及已纠正模式的“正确”输出:
Mongoose: routes.createIndex({ route: '2dsphere' }, { background: true })
Mongoose: routes.deleteMany({}, {})
Mongoose: routes.insertOne({ route: { type: 'MultiLineString', coordinates: [ [ [ -123.3867645, 48.4813423 ], [ -123.3785248, 48.4592626 ], [ -123.3766365, 48.4527165 ], [ -123.3756924, 48.4523749 ], [ -123.3722591, 48.4549366 ], [ -123.3704567, 48.4559612 ] ] ] }, _id: ObjectId("5be9f3915fc64e3548603766"), name: 'A Route', __v: 0 })
Mongoose: routes.findOneAndUpdate({ name: 'A Route' }, { '$push': { 'route.coordinates.0': [ -123.3701134, 48.4467389 ] } }, { upsert: false, remove: false, projection: {}, returnOriginal: false })
{
"route": {
"type": "MultiLineString",
"coordinates": [
[
[
-123.3867645,
48.4813423
],
[
-123.3785248,
48.4592626
],
[
-123.3766365,
48.4527165
],
[
-123.3756924,
48.4523749
],
[
-123.3722591,
48.4549366
],
[
-123.3704567,
48.4559612
],
[
-123.3701134,
48.4467389
]
]
]
},
"_id": "5be9f3915fc64e3548603766",
"name": "A Route",
"__v": 0
}