spring-projects / spring-data-mongodb

Provides support to increase developer productivity in Java when using MongoDB. Uses familiar Spring concepts such as a template classes for core API usage and lightweight repository style data access.
https://spring.io/projects/spring-data-mongodb/
Apache License 2.0
1.62k stars 1.09k forks source link

@TimeSeries collection is ceated as a "collection" type on startup #4792

Open salvatorenovelli opened 1 month ago

salvatorenovelli commented 1 month ago

Hi there, according to Index and Collection Management document page, timeseries collection should be created before inserting data to make sure they're created as a type = "timeseries". (instead of a plain "collection")

As mentioned in the documentation, there are 2 ways to do this:

  1. Using MongoTemplate in conjunction with CreateCollectionOptions.
  2. Using MongoTemplate pulling the option from the @TimeSeries annotation.

Option 1 works, but only if no @TimeSeries annotation is present on the class, but Option 2 does not seem to work at all. Whenever a @TimeSeries annotation is present on the class, any attempt to configure it will fail with an "Collection already exist exception". This seems to be related to the fact that collections are automatically created by MongoPersistentEntityIndexCreator to ensure indexes are created, but this creator seems to ignore the fact the the collection is a TimeSeries and creates it as a plain "collection".

The work-around is quite simple, removing the annotation gives you a chance to create it imperatively, but it doesn't feel the most consistent way given all my other collections are correctly created and configured via annotations.

For reference, in Option 2 I use the code provided in the documentation:

@TimeSeries(collection="weather", timeField = "timestamp")
public class Measurement {
    String id;
    Instant timestamp;
    // ...
}

template.createCollection(Measurement.class);
christophstrobl commented 1 month ago

Thank our for the report. Sounds like we should update the documentation stressing that enabling auto index creation (disabled by default) can interfere with manual collection creation.

salvatorenovelli commented 1 month ago

Fair enough as a position but auto-index creation is a very neat feature. It's not just about creating indexes in a new database, it's also about failing fast, with meaningful exceptions if indexes are created in the wrong way in an existing database. Auto-index creation allows for a reliable database configuration but also run-time assertions based on annotations specified on a single place.

Another option you could consider, if valuable to enough users, could be to provide an option to disable auto-index creation specifically for time-series collections.