MongoDB 2dsphere index error when one embedded document has an empty array for location

In my Ruby on Rails application, I have a User model that embeds_many :addresses. Each Address has a location attribute (an array representing coordinates).

I’ve created a 2dsphere index on the location field of the embedded documents using the following command:

db.users.createIndex({ "addresses.location": "2dsphere" })

Now, here’s the issue:

  • When I create a user with an address where location is an empty array (), the record is saved successfully without any errors.
  • User document:
#<User _id: BSON::ObjectId('62d658f059f8ef319e62004a'), addresses: [{"_id"=>BSON::ObjectId('6821f2ee309473477ed17c37'), "area"=>"Denesik Mountain", "city"=>"Stantonfurt", "country"=>"Spain", "created_at"=>Mon, 12 May 2025 13:09:02.206000000 UTC +00:00, "landmark"=>"Unde reprehenderit sit consequatur.", "line1"=>"20516 Barry Junction Suite 779", "line2"=>"503 Murray Ramp Suite 527", "location"=>[], "other_data"=>{}, "pincode"=>"55456", "reference_id"=>"015327887794519", "route_hash"=>{}, "state"=>"California", "type"=>"primary", "updated_at"=>Mon, 12 May 2025 13:09:02.206000000 UTC +00:00}])> 
  • But when I add another address with a valid location like [12.77, 85.899],
u.addresses << Address.new("location"=>[12.77, 85.899],
 "line1"=>"754 Jerde Corners Suite 281",
 "line2"=>"360 Corrie Court Suite 793",
 "area"=>"Damaris Garden",
 "landmark"=>"Voluptas quod reiciendis amet.",
 "city"=>"Hortensemouth",
 "state"=>"Kansas",
 "country"=>"Monaco",
 "pincode"=>"70369",
 "reference_id"=>"864779509941344",
 "type"=>"primary")

I’m getting the following error:

 .rvm/gems/ruby-3.3.1/gems/mongo-2.19.3/lib/mongo/operation/result.rb:364:in `raise_operation_failure':
[16755]: Can't extract geo keys:
{
  _id: ObjectId('62d658f059f8ef319e62004a'),
  addresses: [
    {
      _id: ObjectId('6821f2ee309473477ed17c37'),
      other_data: {},
      location: [],
      ...
    },
    {
      _id: ObjectId('6822ce1d309473deb7f5af29'),
      other_data: {},
      location: [ 12.77, 85.899 ],
      ...
    }
  ]
}
Point must only contain numeric elements

It seems that the presence of an empty array in any of the embedded addresses causes the 2dsphere index to reject the entire document.

Not all addresses need geolocation. Some addresses may not have a location at all. However, I want to:

  • Allow saving addresses both with and without geolocation.
  • Perform geospatial queries only on addresses that do have valid location data.

How can I model this so that:

  • I don’t get errors when saving addresses without location, and
  • I can still perform geospatial queries on the ones that do?

Is there a way to structure the index or the data to support this scenario?

To summarise the full answer given on StackOverflow:

  1. For a 2dsphere index, the points need to be GeoJSON, not Legacy Coordinate Pairs.

    { type: "Point", coordinates: [ 40, 5 ] }
    
  2. Since your locations are in an array, ‘Missing locations’ should not be an empty object or empty array - the field must not be present. So the first address below should not have the location field. And the second address has location as a GeoJSON Point.

    [
      {
        "_id": { "$oid": "62d658f059f8ef319e62004a" },
        "addresses": [
          {
            "_id": { "$oid": "6821f2ee309473477ed17c37" },
            "other_data": {}
          },
          {
            "_id": { "$oid": "6822ce1d309473deb7f5af29" },
            "other_data": {},
            "location": { "type": "Point", "coordinates": [12.77, 85.899] }
          }
        ]
      }
    ]
    
  3. If you create the index first and then try adding documents which don’t meet the criteria above, they cannot be added. And if the index is created after there are documents which don’t meet te criteria, then the index creation will fail.