【问题标题】:How do I create a MongoDB 2dsphere index on a document containing an array of arrays of coordinates?如何在包含坐标数组的文档上创建 MongoDB 2dsphere 索引?
【发布时间】:2021-11-22 19:46:21
【问题描述】:

我正在尝试在 MongoDB 中为 LineString 对象构建索引,该对象具有一个名为坐标的数组,其中包含两个或多个坐标对数组。

当我运行 db.infrastructure.createIndex({ "geometry.coordinates": "2dsphere" }) 时,我收到以下错误:

MongoServerError: Index build failed: 9ceb3eaa-ff36-48bd-a0a6-cffbc42dcc7a: Collection energy_maps_local_db.infrastructure ( 38a51548-49ea-4ad6-8c90-0cc76e3a1ae7 ) :: caused by :: Can't extract geo keys: { _id: ObjectId('611d22b7bdcefb8992c3f91c'), type: "Feature", properties: { original: { FNODE_: 57, TNODE_: 58, LENGTH: 0.00701013, RAILRDL020: 56, RROWNER1: "Burlington Northern and Santa Fe Railway Company", RROWNER2: null, RROWNER3: null, MARK1: "BNSF", MARK2: null, MARK3: null }, required: { unit: null, viz_dim: null, years: [] }, optional: { description: "" }, type: { primary: "railroads", secondary: null } }, geometry: { type: "LineString", coordinates: [ [ -122.2382307661753, 47.30122202419908 ], [ -122.2312344168241, 47.30166453943731 ] ] } } Point must only contain numeric elements

它说该点必须只包含数字元素,所以我假设它与数组数组有关。

我会尝试使用多索引键,但您似乎无法为多索引 createIndex() 操作指定 2dsphere。如果可能的话,我想避免修改我的数据。

【问题讨论】:

    标签: arrays database mongodb geojson 2dsphere


    【解决方案1】:

    试试db.infrastructure.createIndex({ "geometry": "2dsphere" })。 Mongo 的 2dsphere 支持 GeoJSON 对象或旧坐标对。 geometry 对象是一个 GeoJSON 对象,因为它包含一个 type 字段。 geometry.coordinates 被假定为旧坐标对,但它不能是点数组。

    https://docs.mongodb.com/manual/core/2dsphere/

    【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2021-10-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-03-25
    • 2018-03-30
    • 2017-01-11
    • 1970-01-01
    相关资源
    最近更新 更多