TimeSeriesDsl¶
interface TimeSeriesDsl<Document : Any> : CompoundBsonNode, FieldDsl
Options for HasTimeSeries.timeSeries.
Types¶
Granularity¶
enum Granularity : Enum<TimeSeriesDsl.Granularity>
The granularity of the time-series buckets.
Properties¶
context¶
@LowLevelApi
abstract val context: BsonContext
The context used to generate this expression.
field¶
Converts a Kotlin property into a Field.
Functions¶
accept¶
@LowLevelApi
@DangerousMongoApi
abstract override fun accept(node: BsonNode)
Adds a new node as a child of this one.
acceptAll¶
@LowLevelApi
@DangerousMongoApi
fun <N : Node> CompoundNode<N>.acceptAll(nodes: Iterable<N>)
Adds any number of nodes into this one.
bucketMaxSpan¶
abstract fun bucketMaxSpan(maxSpan: Duration)
Specifies the maximum time between two timestamps in the same bucket.
To learn more about buckets, see metaField.
This option is not compatible with granularity.
This option communicates to MongoDB how long to keep a bucket open. Once this time has passed since the creation of the bucket, MongoDB will close the bucket and create a new one.
The minimum resolution is one second, so values less than one second are rounded up to one second.
This function sets both the bucketMaxSpanSeconds and bucketRoundingSeconds options.
Example
class Notification(
val userId: ObjectId,
val createdAt: Instant,
val text: String,
)
notifications.create {
timeSeries {
timeField(Notification::createdAt)
metaField(Notification::userId)
bucketMaxSpan(12.hours)
}
}
External resources
div¶
Refers to child as a nested field of the current field.
Refers to child as a nested field of the current field.
Refers to child as a nested field of the current field.
expiresAfter¶
abstract fun expiresAfter(duration: Duration)
Specifies how long documents are retained in the collection.
Once this time has passed after the value of the timeField, MongoDB automatically removes the document.
The minimum resolution is one second, so values less than one second are rounded up to one second.
Note. This method sets the ExpiresAfterOption, which does not appear in the toString() representation of this DSL, because MongoDB places this option at the root of the create {} command instead of within the timeseries block.
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
freeze¶
@LowLevelApi
abstract override fun freeze()
Makes this node immutable.
get¶
Refers to a specific item in an array, by its index.
Refers to a specific item in a map, by its name.
Refers to a child item of a field of type BsonArray.
Refers to a child field of a field of type BsonDocument.
@JvmName(name = "bsonField")
operator fun <Child> BsonDocument.Companion.get(field: String): Field<BsonDocument, Child>
Refers to a root field of the document being analyzed.
Refers to a specific item in an array, by its index.
Refers to a specific item in a map, by its name.
Refers to a child item of a field of type BsonArray.
Refers to a child field of a field of type BsonDocument.
granularity¶
abstract fun granularity(granularity: TimeSeriesDsl.Granularity)
Specifies the maximum size of a bucket.
To learn more about buckets, see metaField.
This option is not compatible with bucketMaxSpan.
This option communicates to MongoDB how many documents will be created in each bucket. For example, if metaField is a company ID, and a new document is created by each company every few minutes, choose Granularity.Minutes.
If this option is not specified, Granularity.Seconds is used.
Example
class Notification(
val userId: ObjectId,
val createdAt: Instant,
val text: String,
)
notifications.create {
timeSeries {
timeField(Notification::createdAt)
metaField(Notification::userId)
granularity(Minutes)
}
}
External resources
metaField¶
open fun metaField(field: KProperty1<Document, *>)
Controls how MongoDB buckets documents.
To improve performance, MongoDB stores time-series documents in buckets, with documents closely related to each other in the same bucket for improved cache performance.
Specifying a meta-field is optional but recommended when multiple documents that originated at around the same time are likely to be queried together.
If two documents have the same meta-field value and a similar time-field value, MongoDB stores them together.
A good meta-field is a field that appears in almost all queries and values that are commonly used, for example, the origin of the event or its destination.
Example
class Notification(
val userId: ObjectId,
val createdAt: Instant,
val text: String,
)
notifications.create {
timeSeries {
timeField(Notification::createdAt)
metaField(Notification::userId)
granularity(Minutes)
}
}
In this example, we store notifications destined for some users. We bucket the data by the user each notification is destined for, to ensure we can query all notifications for a given user quickly.
External resources
simplify¶
@LowLevelApi
abstract fun simplify(): BsonNode?
Returns a simplified (but equivalent) expression to the current expression.
timeField¶
open fun timeField(field: KProperty1<Document, Instant>)
Specifies the field which contains the date used to organize this time-series collection.
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
toBson¶
@LowLevelApi
open fun toBson(): BsonDocument
Writes the result of simplifying to a new BsonDocument.
toString¶
unsafe¶
Refers to a field child of the current field, with no compile-time safety.
open infix fun <Root, Child> KProperty1<Root, *>.unsafe(child: KProperty1<*, Child>): Field<Root, Child>
Refers to a field child of the current field, without checking that it is a field available on the current object.
Refers to a field child of the current field, without checking that it is a field available on the current object.
Refers to a field child of the current field, without checking that it is a field available on the current object.
Refers to a field child of the current field, without checking that it is a field available on the current object.
Refers to a field child of the current field, with no compile-time safety.
unsafeCast¶
open fun <Root, NewType> KProperty1<Root, *>.unsafeCast(): Field<Root, NewType>
Overwrites the represented type of this field.
writeTo¶
@LowLevelApi
abstract override fun writeTo(writer: BsonFieldWriter)
Writes the result of simplifying this expression into writer.