Skip to content

HasTimeSeries

Declares time-series collections.

See timeSeries.

Inheritors

Properties

allOptions

@LowLevelApi
abstract val allOptions: List<Option>

The full list of options set on this container.

Specific options are usually searched using the option extension.

context

The context used to generate this expression.

Functions

accept

@LowLevelApi
@DangerousMongoApi
abstract override fun accept(node: BsonNode)

Adds a new node as a child of this one.

acceptAll

Adds any number of nodes into this one.

freeze

@LowLevelApi
abstract override fun freeze()

Makes this node immutable.

option

@LowLevelApi
inline fun <O : Option> Options.option(): O?

Accesses the value of a given Option.

For example, if we have a helper function that sets some default options, and we want to know what maximum limit it set, we can use:

collection.count(
    options = {
        println(option<LimitOption>()) // Will print an integer
    }
)

simplify

@LowLevelApi
abstract fun simplify(): BsonNode?

Returns a simplified (but equivalent) expression to the current expression.

timeSeries

open fun timeSeries(block: TimeSeriesDsl<Document>.() -> Unit)

Time-series are special collections optimized for cases where data is created over time.

Examples:

  • A temperature sensor that makes readings every minute.

  • An audit log where a new document is inserted each time a user makes an action.

  • A log-in log where a new document is inserted on every log-in attempt.

Instead of storing documents independently like in normal collections, documents are grouped together by their source (see TimeSeriesDsl.metaField) in buckets of some time period (see TimeSeriesDsl.granularity and TimeSeriesDsl.bucketMaxSpan), which allows documents from the source and a similar time to be queried together much more efficiently. Additionally, MongoDB can use compression to greatly reduce the disk space used by such a collection.

Time-series documents do not require an _id field.

Example

class Notification(
    val userId: ObjectId,
    val createdAt: Instant,
    val text: String,
)

notifications.create {
    timeSeries {
        timeField(Notification::createdAt)
        metaField(Notification::userId)
        granularity(Minutes)
        expiresAfter(7.days)
    }
}

External resources

Parameters

toBson

Writes the result of simplifying to a new BsonDocument.

toString

abstract override fun toString(): String

JSON representation of this option.

writeTo

@LowLevelApi
abstract override fun writeTo(writer: BsonFieldWriter)

Writes the result of simplifying this expression into writer.