首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >猫鼬5.3.8 -无法用MultiLineString和$push提取Geo键

猫鼬5.3.8 -无法用MultiLineString和$push提取Geo键
EN

Stack Overflow用户
提问于 2018-11-12 05:53:25
回答 1查看 708关注 0票数 1

我正试图将一个坐标对转换为一个包含嵌套如下的GeoJSON MultiLineString的文档:[[Number]]

当我findOneAndUpdate喜欢这样的时候:

req.body.point = -123.347,48.43649

代码语言:javascript
复制
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)
})

我收到错误消息:

代码语言:javascript
复制
errmsg": "Can't extract geo keys: { _id: ObjectId('5be9eef393311d2d2c6130fd').......
Point must only contain numeric elements",
"code": 16755,

我的坐标是有效的长,lat格式,如芒戈所期望的。这里有什么东西我遗漏了吗?

这是我的MultiLineString模式:

代码语言:javascript
复制
const MultiLineStringSchema = new Schema({
  type: {
    type: String,
    enum: ['MultiLineString'],
    required: true,
    default: 'MultiLineString'
  },
  coordinates: {
    type: [[[Number]]],
    required: true
  }
});

这是我的路由模式:

代码语言:javascript
复制
var { MultiLineStringSchema } = require('./GeoJson-Schemas')

const RouteSchema = new mongoose.Schema({
  name: String,
  route: {
    type: MultiLineStringSchema,
    required: true
  }
});
RouteSchema.index({ route: "2dsphere" });

编辑2:这里的是保存错误的路由文档。我已经用这个文档重新创建了错误,并在上面的错误消息中更新了匹配的_id。

代码语言:javascript
复制
{
    "_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
}

向前走一步,我已经将{_id: false}选项添加到MultiLineString子文档中,并重新初始化了一个新集合。在像这样存储新文档时:

代码语言:javascript
复制
{
    "_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
}

我试图使用精确的语法来findOneAndUpdate:

代码语言:javascript
复制
Route.findOneAndUpdate({name},
    {'$push': { "route.coordinates.0": point}}, {new: true})
    .then(doc => {
        return res.send(doc)
    }).catch(err => {
        res.send(err)
    })

并导致同样的错误:

代码语言:javascript
复制
"errmsg": "Can't extract geo keys: { _id: ObjectId('5be9f076e1caaa23682d80de')...
Point must only contain numeric elements",
"code": 16755,

编辑3:

我再次更新了RouteSchema 删除子文档,如下所示:

代码语言:javascript
复制
const RouteSchema = new mongoose.Schema({
  name: String,
  route: {
    type: {
      type: String,
      enum: ['MultiLineString'],
      required: true,
      default: 'MultiLineString'
    },
    coordinates: {
      type: [[[Number]]],
      required: true
    }
  }
});

我将文档存储如下:

代码语言:javascript
复制
{
    "_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的语法相同,并收到相同的错误。

代码语言:javascript
复制
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:

collection PHOTO

在shell连接上运行db.routes.getIndexes()提供了以下内容。

代码语言:javascript
复制
 [
            {
                    "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
            }
    ]
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2018-11-13 02:08:31

因此,这个问题确实是可重复的,可以归结为模式定义。就在这里:

代码语言:javascript
复制
coordinates: {
  type: [[[Number]]],
  required: true
}

这就是您正在努力指定文档的MultiLineString格式所需的嵌套数组“环”的地方。这似乎是正确的,插入新文档并不是问题,但请注意MongoDB想要的符号如下:

代码语言:javascript
复制
{ $push: { 'route.coordinates.0': [-123.3701134, 48.4467389] } }

因此,“模式”有一个“双数组嵌套”,就像在[[[Number]]]中一样,但是MongoDB只想要coordinates.0,而不是coordinates.0.0,以获得快乐。这就是猫鼬感到困惑并“重写”update语句的地方:

代码语言:javascript
复制
{ '$push': { 'route.coordinates.0': [ [ -123.3701134 ], [ 48.4467389 ] ] } }

所以这是错误的,也是错误的根源:

_id: ObjectId(\‘5be9f3915fc64e3548603766’),路由:{ type:"MultiLineString",坐标:[ -123.3867645,48.4813423,-123.3785248,48.4592626,-123.3766365,48.4527165,-123.3756924,48.4523749,-123.3722591,48.4523749,-123.3704567,48.4523749,[ -123.3701134 ]] }名称:"A路“,__v: 0} Point必须只包含数字元素‘,

将模式更改为“如此特定”,可以防止猫鼬“弄坏”语句:

代码语言:javascript
复制
coordinates: {
  type: []   // Just expect an array without any other definition
  required: true
}

然后,更新按预期进行。

为了获得完整的可复制列表:

代码语言:javascript
复制
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();
  }

})()

以及更正模式的“正确”输出:

代码语言:javascript
复制
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
}
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/53256539

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档