本文介绍了如何在包含坐标数组数组的文档上创建MongoDB 2dsphere索引?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

当我运行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。如果可能,我希望避免修改我的数据。

推荐答案

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

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

这篇关于如何在包含坐标数组数组的文档上创建MongoDB 2dsphere索引?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-18 13:35