Why doesn't Spring Data MongoDB create a time series collection during direct insertion with @TimeSeries annotation?

I’m using Spring Data MongoDB to interact with a MongoDB database. In my application, I have a Test class annotated as a time series collection using the @TimeSeries annotation as follows:

TimeSeries(
    timeField = "timestamp",
    granularity = Granularity.SECONDS,
    metaField = "deviceId")
public class Test {

  @Id private String id;
  private String deviceId;
  private OffsetDateTime timestamp;
  private int measurement;
  .....

  }

When I explicitly use the mongoTemplate.createCollection(Test.class) method, the collection is created as a time series collection as expected. However, if I directly insert data without pre-creation like:

List<Test> testObjectsList = //list of Test objects;
mongoTemplate.insertAll(testObjectsList);

It results in a standard collection, despite having the class annotated with @TimeSeries.

Why does this happen? Why doesn’t MongoDB recognize the @TimeSeries annotation during direct insertion?

Hello, welcome to the MongoDB community!

This is actually intentional, this annotation is from Spring Data MongoDB and not something native to MongoDB. When you try to insert data without the collection being created, MongoDB will create a default collection, as the insert is supposed to be quick and light, without examining metadata or any configuration.

In this case, always create your collection in advance before inserting data, so that you can be sure that the settings passed were accepted correctly.