spring-projects / spring-projects/spring-data-mongodb
Allow creating time series collections with custom name and CollectionOptions derived from annotation
Nobody has claimed this yet.
- Dominant language
- Java
- Stars
- 1.7k
- Forks
- 1.1k
- PR merge metrics
- No merged PRs in 30d
Description
According to the documentation, it is possible to create a time series collection via MongoTemplate by either providing explicit CollectionOptions or a class annotated with @TimeSeries:
@TimeSeries(timeField = "timestamp")
record MyTimeSeries(@Id String id, Instant timestamp, String value) {}
mongoTemplate.createCollection(MyTimeSeries.class)
in which case the CollectionOptions are derived from the annotation: https://github.com/spring-projects/spring-data-mongodb/blob/b8b93bcf93050d923aadef60a8142eda03c8f62f/spring-data-mongodb/src/main/java/org/springframework/data/mongodb/core/EntityOperations.java#L1058-L1081
However, if you want to create the collection with a custom name (not derived from the class), there is currently no option for deriving the CollectionOptions from the annotation. The only overloads of mongoTemplate.createCollection that take a String collectionName also require explicit CollectionOptions.
Using mongoTemplate.save(value, "myCustomName") is a non-starter since it does not read the entity metadata and just creates a basic collection (not a time series) with no other options.
Custom names are important for several widespread use cases, for example collection-level multi-tenancy, where you would create multiple collection with the same definition to segregate data for different tenants, or any other types of data segmentation.
I propose new methods to create a collection that take both the collectionName and the entityClass as inputs:
public <T> MongoCollection<Document> createCollection(String collectionName, Class<T> entityClass) {
return createCollection(collectionName, entityClass,
operations.forType(entityClass).getCollectionOptions());
}
public <T> MongoCollection<Document> createCollection(String collectionName, Class<T> entityClass,
@Nullable CollectionOptions collectionOptions) {
Assert.notNull(collectionName, "CollectionName must not be null");
Assert.notNull(entityClass, "EntityClass must not be null");
return doCreateCollection(collectionName,
operations.convertToCreateCollectionOptions(collectionOptions, entityClass));
}
Contributor guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Research direction
Start with the MongoTemplate createCollection overloads and the EntityOperations collection-options logic linked in the issue. Trace how annotation-derived CollectionOptions are obtained, then review existing collection-management tests or entry points before adding coverage for a custom name and entity class; done means the overload derives the annotated options while preserving explicit options behavior.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- java, mongodb, spring
- Domain
- database
- Issue type
- Feature
- Difficulty
- 3/5
- Estimated time
- 1-2 days
- Activity status
- Stale
- Clarity
- Mostly clear
- Newbie friendliness
- 48/100