-
Notifications
You must be signed in to change notification settings - Fork 1.1k
Description
I have an object type that is stored in MongoDB under two tables: one using the default (class) table name, another using a custom name. The code needs to ensure a geospatial index exists on the table before doing a “near” query. It looks something like this:
getMongoTemplate().indexOps("topPlaces", Place.class).ensureIndex(
new GeospatialIndex("location")
.typed(GeoSpatialIndexType.GEO_2DSPHERE));
GeoResults<Place> results = getMongoTemplate()
.geoNear(nearQuery, Place.class, "topPlaces");
When executing the query, it fails with:
$geoNear requires a 2d or 2dsphere index, but none were found' on server localhost:27017
and checking the tables in mongosh I found the index was created on the “place” table, not on “topPlaces”. I traced the fault down to this line in DefaultIndexOperations.execute(callback):
Line 212 in 7821c2a
| if (type != null) { |
which prioritizes executing the callback for the type of the collection if present over the collectionName.
If this is the intended behavior of indexOps(collectionName, type), it’s not documented. The javadoc implies that the type parameter is only used for field mapping. It also says that collectionName is required, even though it isn’t used.
(Caveat: I’m currently running with spring-data-mongodb 4.0.12, but it appears this execute method has not been changed in the latest code here on GitHub.)