Announcement Announcement Module
Collapse
No announcement yet.
Mongo error when executing Geospatial query Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • Mongo error when executing Geospatial query

    I'm trying to run some simple integration tests which store some Checkin domain objects in a Mongo collection, and then run a simple geospatial query to return some based on a particular location, however, mongod throws the below error once the query is run:

    Code:
    can't find special index: 2d for: { location: { $within: { $center: [ [ 34.0269
    , -118.4737 ], 0.01 ] } } }; nested exception is com.mongodb.MongoException: can't find special index: 2d for: { 
    location: { $within: { $center: [ [ 34.0269, -118.4737 ], 0.01 ] } } }

    Domain object code:
    Code:
    @Document(collection = "checkin")
    public class Checkin implements ICheckin {
    
        @Id
        private String id;
    
        @GeoSpatialIndexed
        private double[] location;
    
        public double[] getLocation() {
            return location;
        }
    
        public void setLocation(double[] location) {
            this.location = location;
        }
    
    ..

    Custom query for checkins by location:
    Code:
        @Autowired
        private MongoTemplate mongoTemplate;
    
        public List<Checkin> findByLocation(double lat, double lon, double radius) {
    
            mongoTemplate.ensureIndex(new GeospatialIndex("location"));
    
            Circle circle = new Circle(lat, lon, radius);
            List<Checkin> checkins =
                mongoTemplate.find(new Query(Criteria.where("location").withinCenter(circle)), Checkin.class);
    
            return checkins;
        }
    I can tell my index is not being created for some reason, but what am I doing wrong?

  • #2
    Have you configured "checkin" as default collection on MongoTemplate? If not you have to use ensureIndex("checkin", new GeospatialIndex("location")). There should also be a method overload that takes a Class<?> instead of a collection name which in turn uses the mapping metadata to determine the collection to create the index for.

    I think we did some refactorings on that post M2 release so you might want to use a current snapshot to see the methods I just mentioned.

    Comment


    • #3
      Originally posted by Oliver Gierke View Post
      Have you configured "checkin" as default collection on MongoTemplate? If not you have to use ensureIndex("checkin", new GeospatialIndex("location")). There should also be a method overload that takes a Class<?> instead of a collection name which in turn uses the mapping metadata to determine the collection to create the index for.

      I think we did some refactorings on that post M2 release so you might want to use a current snapshot to see the methods I just mentioned.
      Thanks. In that particular integration test, I'd set the default collection to another one. Setting it back to "checkin" fixed the issue. Your other suggestion to pass the collection name in to the ensureIndex call also worked.

      Comment

      Working...
      X