Automatic Time-Series Collection Creation
Use annotations to automatically create time-series collections, eliminating manual creation.
Configuration
- To use MongoPlus's automatic time-series collection creation, enable the configuration.
- For multi-module projects with different package names, specify the subpackage paths containing time-series collection classes in the main program configuration file.
yaml
mongo-plus:
configuration:
auto-create-time-series: true # Enable automatic time-series collection creation
auto-scan-packages: # Specify subpackage paths for multi-module projects
- com.xxxx.entity
- com.xxxx.entity1
Annotation
@TimeSeries
- Automatically creates time-series collections using annotations.
- Apply this annotation to specify that the entity class represents a time-series collection.
- Can interact with
@CollectionName
annotation, using the collection name and database properties defined there.
Property | Type | Required | Default | Description |
---|---|---|---|---|
dataSource | String | No | Current context | Specifies the data source for the time-series collection |
timeField | String | Yes | Field name containing the timestamp for each time-series document | |
metaField | String | No | "" | Field name containing metadata for each time-series document |
granularity | TimeSeriesGranularity | No | SECONDS | Specifies the granularity of the time-series |
bucketMaxSpan | long | No | -1 | Maximum time span between measurements in a bucket, in seconds |
bucketRounding | long | No | -1 | Maximum time span between measurements in a bucket, in seconds |
Example
java
@TimeSeries(timeField = "$ts", metaField = "testMeta", granularity = TimeSeriesGranularity.SECONDS)
@Data
@AllArgsConstructor
@NoArgsConstructor
public class MongoPlus {
@ID
private String id;
@CollectionField("ts_test")
private LocalDateTime ts;
private Map<String, Object> testMeta;
private String name;
}
TIP
timeField
and metaField
properties can use {@code $} to reference a field in the class. If the field does not exist, the value is used directly. For example:
@TimeSeries(metaField="$field1")