Skip to main content

Filtering Time Series Queries

Filter by value

  • A time series entry can have up to 32 values.

  • A time series query can filter entries based on these values.

// For example, in the "HeartRates" time series,
// retrieve only entries where the value exceeds 75 BPM

var baseTime = new DateTime(2020, 5, 17, 00, 00, 00);
var from = baseTime;
var to = baseTime.AddMinutes(10);

var query = session.Query<Employee>()
.Select(employee => RavenQuery
.TimeSeries(employee, "HeartRates", from, to)
// Call 'Where' to filter entries by the value
.Where(ts => ts.Value > 75)
.ToList());

var results = query.ToList();

Filter by tag

  • A time series entry can have an optional tag.

  • A time series query can filter entries based on this tag.

// Retrieve only entries where the tag string content is "watches/fitbit"

var baseTime = new DateTime(2020, 5, 17, 00, 00, 00);
var from = baseTime;
var to = baseTime.AddMinutes(10);

var query = session.Query<Employee>()
.Select(employee => RavenQuery
.TimeSeries(employee, "HeartRates", from, to)
// Call 'Where' to filter entries by the tag
.Where(ts => ts.Tag == "watches/fitbit")
.ToList());

var results = query.ToList();
// Retrieve only entries where the tag string content is one of several options

var baseTime = new DateTime(2020, 5, 17, 00, 00, 00);
var from = baseTime;
var to = baseTime.AddMinutes(10);

var optionalTags = new[] {"watches/apple", "watches/samsung", "watches/xiaomi"};

var query = session.Query<Employee>()
.Select(employee => RavenQuery
.TimeSeries(employee, "HeartRates", from, to)
// Call 'Where' to filter entries by the tag
.Where(ts =>
ts.Tag == "watches/apple" || ts.Tag == "watches/samsung" || ts.Tag == "watches/xiaomi")
.ToList());

var results = query.ToList();

Filter by referenced document

  • A time series entry's tag can contain the ID of a document.

  • A time series query can filter entries based on the contents of this referenced document.
    The referenced document is loaded, and entries are filtered by its properties.

// Retrieve entries that reference a document that has "Sales Manager" in its 'Title' property

var query = session.Query<Company>()
// Query companies from USA
.Where(c => c.Address.Country == "USA")
.Select(company => RavenQuery
.TimeSeries(company, "StockPrices")
// Use 'LoadByTag' to load the employee document referenced in the tag
.LoadByTag<Employee>()
// Use 'Where' to filter the entries by the 'Title' property of the loaded document
.Where((ts, employeeDoc) => employeeDoc.Title == "Sales Manager")
.ToList());

var results = query.ToList();