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.

Parameter Type Explanation
doc string
or
document instance
Document ID, e.g. timeseries('users/1-A', 'StockPrice')

e.g. timeseries(this, 'StockPrice')
name string Time 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:

    Parameter Type Explanation
    timestamp DateTime Timestamp
    values double[] Values
    tag string Tag

timeseries.delete (from, to)

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

Parameter Type Explanation
from (optional) DateTime Range Start
Default: DateTime.Min
to (optional) DateTime Range End
Default: DateTime.Max

timeseries.get (from, to)

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

  • Parameters:

    Parameter Type Explanation
    from (optional) DateTime Range Start
    Default: DateTime.Min
    to (optional) DateTime Range 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

  • In this sample, we pass session.Advanced.Defer a script that appends a document 100 time series entries.

    session.Advanced.Defer(new PatchCommandData("users/1-A", 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.

    // Delete time-series from all users
    PatchByQueryOperation deleteOperation = new PatchByQueryOperation(new IndexQuery
    {
        Query = @"from Users as u
                    update
                    {
                        timeseries(u, $name).delete($from, $to)
                    }",
        QueryParameters = new Parameters
                {
                    { "name", "HeartRates" },
                    { "from", DateTime.MinValue },
                    { "to", DateTime.MaxValue }
                }
    });
    store.Operations.Send(deleteOperation);