Why am I getting "Point must only contain numeric elements" using MongoDB MultiPoint example?

96 Views Asked by At

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

There are 1 best solutions below

0
SnowBeef On

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
      ]
    ]
  }
}