Skip to main content

Time Series: JavaScript Support

With the introduction of time series, RavenDB's support for the execution of JavaScript-based operations over single and multiple documents has been extended to allow manipulations involving time series.

JavaScript API methods

The JavaScript time series API includes these methods:

timeseries (doc, name)

Choose a time series by the ID of its owner document and by the series name.

ParameterTypeExplanation
docstring
or
document instance
Document ID, e.g. timeseries('users/1-A', 'StockPrice')

e.g. timeseries(this, 'StockPrice')
namestringTime Series Name

timeseries.append

  • You can use two overloads, to append tagged or untagged time series entries.

    • timeseries.append (timestamp, values)
    • timeseries.append (timestamp, values, tag)
  • Parameters:

    ParameterTypeExplanation
    timestampDateTimeTimestamp
    valuesdouble[]Values
    tagstringTag

timeseries.delete (from, to)

Use this method to delete a range of entries from a document.

ParameterTypeExplanation
from (optional)DateTimeRange Start
Default: DateTime.Min
to (optional)DateTimeRange End
Default: DateTime.Max

timeseries.get (from, to)

Use this method to retrieve a range of time series entries.

  • Parameters:

    ParameterTypeExplanation
    from (optional)DateTimeRange Start
    Default: DateTime.Min
    to (optional)DateTimeRange End
    Default: DateTime.Max
  • Return Type:
    Values are returned in an array of time series entries, i.e. -

[
{
"Timestamp" : ...
"Tag": ...
"Values": ...
"IsRollup": ...
},
{
"Timestamp" : ...
"Tag": ...
"Values": ...
"IsRollup": ...
}
..
]

Usage Samples

var baseTime = DateTime.UtcNow;

// Create arrays of timestamps and random values to patch
var values = new List<double>();
var timeStamps = new List<DateTime>();

for (var i = 0; i < 100; i++)
{
values.Add(68 + Math.Round(19 * new Random().NextDouble()));
timeStamps.Add(baseTime.AddMinutes(i));
}

session.Advanced.Defer(new PatchCommandData("users/john", null,
new PatchRequest
{
Script = @"
var i = 0;
for(i = 0; i < $values.length; i++)
{
timeseries(id(this), $timeseries)
.append (
new Date($timeStamps[i]),
$values[i],
$tag);
}",

Values =
{
{ "timeseries", "HeartRates" },
{ "timeStamps", timeStamps },
{ "values", values },
{ "tag", "watches/fitbit" }
}
}, null));

session.SaveChanges();
  • In this sample, we pass PatchByQueryOperation a script that runs a document query and deletes the HeartRate time series from matching documents.
PatchByQueryOperation deleteByQueryOp = new PatchByQueryOperation(new IndexQuery
{
Query = @"from Users as u
where u.Age < 30
update
{
timeseries(u, $name).delete($from, $to)
}",

QueryParameters = new Parameters
{
{ "name", "HeartRates" },
{ "from", DateTime.MinValue },
{ "to", DateTime.MaxValue }
}
});

// Execute the operation:
// Time series "HeartRates" will be deleted for all users with age < 30
store.Operations.Send(deleteByQueryOp);