Skip to content
广告❤️成为赞助商

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.
PropertyTypeRequiredDefaultDescription
dataSourceStringNoCurrent contextSpecifies the data source for the time-series collection
timeFieldStringYesField name containing the timestamp for each time-series document
metaFieldStringNo""Field name containing metadata for each time-series document
granularityTimeSeriesGranularityNoSECONDSSpecifies the granularity of the time-series
bucketMaxSpanlongNo-1Maximum time span between measurements in a bucket, in seconds
bucketRoundinglongNo-1Maximum 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")