0

I'm trying to figure out why I can't create this 2dsphere index in my MongoDB collection.

I took this object example directly from the MongoDB docs and placed it into a new collection.

{
  type: "MultiPoint",
  coordinates: [
     [ -73.9580, 40.8003 ],
     [ -73.9498, 40.7968 ],
     [ -73.9737, 40.7648 ],
     [ -73.9814, 40.7681 ]
  ]
}

I try to create a new 2dsphere index based on the coordinates field, and I get this error:

Index build failed: e278714f-7ed3-438f-9794-b6c17dbc1e99: 
Collection UltimateTravelBuddy.locationmultipoints ( f42e2c68-6c16-4881-91a7-9e45b0348029 ) :: 
caused by :: 
Can't extract geo keys: { _id: ObjectId('651cdbcab5d1c724f0fb88fa'), type: "MultiPoint", coordinates: [ [ -73.958, 40.8003 ], [ -73.9498, 40.7968 ], [ -73.97369999999999, 40.7648 ], [ -73.98139999999999, 40.7681 ] ] } 
Point must only contain numeric elements, instead got type array

Am I missing something obvious, or are indexes on MultiPoint objects not actually supported?

I'm running MongoDB 7.0.1 Community.

1 Answer 1

2

Figured it out, the index has to be pointed at the complete object, which includes both the type and coordinates. I created a parent object (multipointobj), pointed the index at that, and the index was created successfully.

{
  "_id": {
    "$oid": "651cdf36b5d1c724f0fb88ff"
  },
  "multipointobj": {
    "type": "MultiPoint",
    "coordinates": [
      [
        -73.958,
        40.8003
      ],
      [
        -73.9498,
        40.7968
      ],
      [
        -73.9737,
        40.7648
      ],
      [
        -73.9814,
        40.7681
      ]
    ]
  }
}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.