Results 1 to 3 of 3

Thread: Mongo error when executing Geospatial query

  1. #1
    Join Date
    Apr 2005
    Posts
    13

    Default 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. #2
    Join Date
    Apr 2006
    Location
    Dresden, Germany
    Posts
    493

    Default

    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.

  3. #3
    Join Date
    Apr 2005
    Posts
    13

    Default

    Quote 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.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •