1

I Have this collection :

{
  geopositions: [
          {type:"Point", coordinates: [-10, 12]},
          {type:"Point", coordinates: [-25, 85]}
     ]
}

but when i try to create the 2DSphere index on geopositions with db.myCollection.createIndex( {"geopositions": "2dsphere"} ); I receive an error (Can't extract geo keys). How can i create my 2DSphere index ?

zeus
  • 12,173
  • 9
  • 63
  • 184
  • need array because the objects can have several location assigned (ex: several offices) – zeus May 04 '18 at 08:22
  • Well if it's something like that then it even makes sense to have a sub-property. Such just do `"geopositions": [{ "key": "office", "location": { "type": "Point", "coordinates": [-10, 12] } },{ "key": "home", "location": { "type": "Point", "coodinates": [-25,85] ] } }]` Or even something as inane as `id` ( which probably really makes sense as a unique identifier ). As long as you have actual valid coordinate values, then it will be fine. But as per your current structure it looks like a "malformed geometry". So put it under `"location"` and make the index `"geopositions.location"` instead – Neil Lunn May 04 '18 at 08:28
  • thanks Neil, yes i can add sub-property however i think it's little redundant :( their is no other way ? – zeus May 04 '18 at 09:02
  • Like I said if you are passing an array of "Points" MongoDB thinks it's a malformed geometry. But like I said, a "unique identifier" is often something you actually do want in an array anyway. You could raise a JIRA, but I don't see it getting a favorable response. And I also said there are limitations to putting such data in an array anyway, so be sure it suits your needs first or otherwise put it in a separate collection. – Neil Lunn May 04 '18 at 09:16
  • @loki did you find any solution for 2DSphere index on array? – sridharnetha Sep 09 '19 at 10:39

0 Answers0