Patch Time Series Entries



Usage

  • Open a session
  • Construct a PatchCommandData instance and pass it the following:
    • The document ID that contains the time series
    • The document change vector (or null)
    • A PatchRequest instance with a JavaScript that appends or removes time series entries
  • Call session.Advanced.Defer and pass it the PatchCommandData command.
    Note that you can call Defer multiple times prior to calling SaveChanges.
  • Call session.SaveChanges().
    All patch requests added via Defer will be sent to the server for execution when SaveChanges is called.

Patching examples

Append multiple entries:

In this example, we append 100 time series entries with random heart rate values to a document.

var baseline = DateTime.Today;

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

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

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();

Delete multiple entries:

In this example, we remove a range of 50 time series entries from a document.

// Delete time-series entries
session.Advanced.Defer(new PatchCommandData("users/1-A", null,
    new PatchRequest
    {
        Script = @"timeseries(this, $timeseries)
                 .delete(
                    $from,
                    $to
                  );",
        Values =
        {
            { "timeseries", "HeartRates" },
            { "from", baseline.AddSeconds(0) },
            { "to", baseline.AddSeconds(49) }
        }
    }, null));

session.SaveChanges();

Syntax

PatchCommandData

public PatchCommandData(string id, string changeVector,
    PatchRequest patch, PatchRequest patchIfMissing)

Learn more about PatchCommandData here.


PatchRequest

public class PatchRequest
{
    // The patching script     
    public string Script { get; set; }
    
    // Values for the parameters used by the patching script
    public Dictionary<string, object> Values { get; set; }
}

Learn more about PatchRequest here.