spring-projects / spring-projects/spring-data-mongodb

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

Open
#4,792 2 comments 1 reaction 1 assignee View on GitHub

@christophstrobl is already working on this.

Since Sep 26, 2024.

type: documentation
Dominant language
Java
Stars
1.7k
Forks
1.1k
PR merge metrics
No merged PRs in 30d

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:

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

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.