Description
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:
- Using
MongoTemplate
in conjunction withCreateCollectionOptions
. - 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);