0

I am trying to build an index in MongoDB for a LineString object that has an array called coordinates that contains two or more arrays of coordinate pairs.

When I run db.infrastructure.createIndex({ "geometry.coordinates": "2dsphere" }) I get the following error:

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

It says the point must contain only numeric elements, so I'm assuming it has something to do with the array of arrays.

I would try using a multi-index key but it doesn't appear that you can specify 2dsphere for multi-index createIndex() operations. I would like to avoid modifying my data if at all possible.

bkleeman
  • 109
  • 1
  • 14

1 Answers1

2

Try db.infrastructure.createIndex({ "geometry": "2dsphere" }). Mongo's 2dsphere supports either GeoJSON objects or legacy coordinate pairs. The geometry object is a GeoJSON object because it contains a type field. geometry.coordinates is assumed to be a legacy coordinate pair, but that can't be an array of points.

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

N Matteson
  • 53
  • 5
  • When I run that I get the following error: `Index build failed: 0806eff0-2dc3-41a9-bdf1-b01d8512e186: Collection energy_maps_local_db.infrastructure ( 38a51548-49ea-4ad6-8c90-0cc76e3a1ae7 ) :: caused by :: Can't extract geo keys: ... ... Duplicate vertices: 272 and 275` – bkleeman Nov 22 '21 at 20:40
  • 1
    I believe that just means you have duplicate points in an object you're trying to index (and indexes have to be unique). Probably the data needs to be cleaned before ingestion and indexing. This other question might be relevant: [https://stackoverflow.com/questions/46382135/using-spring-data-mongodb-how-can-i-avoid-duplicate-vertices-error](https://stackoverflow.com/questions/46382135/using-spring-data-mongodb-how-can-i-avoid-duplicate-vertices-error) – N Matteson Nov 22 '21 at 20:45
  • 1
    This strategy should work for removing duplicate points: [https://stackoverflow.com/questions/53389236/how-to-remove-duplicate-values-coordinates-in-a-list](https://stackoverflow.com/questions/53389236/how-to-remove-duplicate-values-coordinates-in-a-list). – N Matteson Nov 22 '21 at 20:51
  • 1
    Rather than deleting duplicate points, you could also split the `LineString` into a `MultiLineString` (I think), so that the duplicated points were each in their own `LineString`. See this issue [https://jira.mongodb.org/browse/SERVER-20672](https://jira.mongodb.org/browse/SERVER-20672). – N Matteson Nov 22 '21 at 20:59
  • 1
    You might also consider redeclaring those as `MultiPoint` if you just need to index the individual points, and not the line/polygon. – Joe Nov 23 '21 at 01:51