bkleeman
bkleeman

Reputation: 119

How do I create a MongoDB 2dsphere index on a document containing an array of arrays of coordinates?

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.

Upvotes: 0

Views: 975

Answers (1)

N Matteson
N Matteson

Reputation: 53

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/.

Upvotes: 2

Related Questions