Skip to content

MongoTemplate.indexOps(collectionName, type) ignores collectionName when type is not null #4698

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
Typraeurion opened this issue May 15, 2024 · 1 comment
Assignees
Labels
type: bug A general bug

Comments

@Typraeurion
Copy link

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):

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

@spring-projects-issues spring-projects-issues added the status: waiting-for-triage An issue we've not yet triaged label May 15, 2024
@mp911de mp911de added type: bug A general bug and removed status: waiting-for-triage An issue we've not yet triaged labels May 16, 2024
@mp911de mp911de self-assigned this May 16, 2024
@mp911de
Copy link
Member

mp911de commented May 16, 2024

execute(Class<?> entityClass, CollectionCallback<T> callback) merely derives the collection name from a given type. While this can be useful for cases where the collection name is dynamic (e.g. by using SpEL expressions), we should rather capture the collection name that was given during index operations creations. The reactive counterpart of index operations sticks already to the given collection name.

That being said, I consider this behavior a bug.

@mp911de mp911de changed the title MongoTemplate.indexOps(collectionName, type) ignores collectionName when type is not null MongoTemplate.indexOps(collectionName, type) ignores collectionName when type is not null May 16, 2024
christophstrobl pushed a commit that referenced this issue May 16, 2024
@christophstrobl christophstrobl added this to the 4.2.6 (2023.1.6) milestone May 16, 2024
natedanner pushed a commit to natedanner/spring-projects__spring-data-mongodb that referenced this issue May 20, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
type: bug A general bug
Projects
None yet
4 participants