Class Windows

java.lang.Object
com.mongodb.client.model.Windows

@Beta public final class Windows extends Object
Builders for windows used when expressing windowed computations. There are two types of windows: documents and range.

Bounded and half-bounded windows require sorting. Window bounds are inclusive and the lower bound must always be less than or equal to the upper bound. The following type-specific rules are applied to windows:

  • documents
    • bounds
      • 0 refers to the current document and is functionally equivalent to Windows.Bound.CURRENT;
      • a negative value refers to documents preceding the current one;
      • a positive value refers to documents following the current one;
  • range
    • sortBy
      • must contain exactly one field;
      • must specify the ascending sort order;
      • the sortBy field must be of either a numeric BSON type (see the $isNumber aggregation pipeline stage) or the BSON Date type if time bounds are used;
    • bounds
      • if numeric, i.e., not Windows.Bound, then the bound is calculated by adding the value to the value of the sortBy field in the current document;
      • if Windows.Bound.CURRENT, then the bound is determined by the current document and not the current value of the sortBy field;
      • time bounds require specifying a time unit and are added as per the $dateAdd/$dateSubtract aggregation pipeline stage specification.
Since:
4.3
See Also:
MongoDB documentation
$isNumber aggregation pipeline stage
BSON Date type
$dateAdd aggregation pipeline stage
$dateSubtract aggregation pipeline stage
Since server release
5.0
  • Nested Class Summary

    Nested Classes
    Modifier and Type
    Class
    Description
    static enum 
    Special values that may be used when specifying the bounds of a window.
  • Method Summary

    Modifier and Type
    Method
    Description
    static Window
    documents(int lower, int upper)
    Creates a documents window whose bounds are determined by a number of documents before and after the current document.
    static Window
    documents(int lower, Windows.Bound upper)
    Creates a documents window whose bounds are determined by a number of documents before and after the current document.
    static Window
    documents(Windows.Bound lower, int upper)
    Creates a documents window whose bounds are determined by a number of documents before and after the current document.
    static Window
    Creates a documents window whose bounds are determined by a number of documents before and after the current document.
    static Window
    of(Bson window)
    Creates a window from Bson in situations when there is no builder method that better satisfies your needs.
    static Window
    range(double lower, double upper)
    Creates a dynamically-sized range window whose bounds are determined by a range of possible values around the value of the sortBy field in the current document.
    static Window
    range(double lower, Windows.Bound upper)
    Creates a dynamically-sized range window whose bounds are determined by a range of possible values around the value of the sortBy field in the current document.
    static Window
    range(long lower, long upper)
    Creates a dynamically-sized range window whose bounds are determined by a range of possible values around the value of the sortBy field in the current document.
    static Window
    range(long lower, Windows.Bound upper)
    Creates a dynamically-sized range window whose bounds are determined by a range of possible values around the value of the sortBy field in the current document.
    static Window
    range(Windows.Bound lower, double upper)
    Creates a dynamically-sized range window whose bounds are determined by a range of possible values around the value of the sortBy field in the current document.
    static Window
    range(Windows.Bound lower, long upper)
    Creates a dynamically-sized range window whose bounds are determined by a range of possible values around the value of the sortBy field in the current document.
    static Window
    Creates a dynamically-sized range window whose bounds are determined by a range of possible values around the value of the sortBy field in the current document.
    static Window
    Creates a dynamically-sized range window whose bounds are determined by a range of possible values around the value of the sortBy field in the current document.
    static Window
    range(Decimal128 lower, Decimal128 upper)
    Creates a dynamically-sized range window whose bounds are determined by a range of possible values around the value of the sortBy field in the current document.
    static Window
    timeRange(long lower, long upper, MongoTimeUnit unit)
    Creates a dynamically-sized range window whose bounds are determined by a range of possible values around the BSON Date value of the sortBy field in the current document.
    static Window
    timeRange(long lower, MongoTimeUnit unit, Windows.Bound upper)
    Creates a dynamically-sized range window whose bounds are determined by a range of possible values around the BSON Date value of the sortBy field in the current document.
    static Window
    timeRange(Windows.Bound lower, long upper, MongoTimeUnit unit)
    Creates a dynamically-sized range window whose bounds are determined by a range of possible values around the BSON Date value of the sortBy field in the current document.

    Methods inherited from class java.lang.Object

    clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
  • Method Details

    • of

      public static Window of(Bson window)
      Creates a window from Bson in situations when there is no builder method that better satisfies your needs. This method cannot be used to validate the syntax.

      Example
      The following code creates two functionally equivalent windows, though they may not be equal.

      
        Window pastWeek1 = Windows.timeRange(-1, MongoTimeUnit.WEEK, Windows.Bound.CURRENT);
        Window pastWeek2 = Windows.of(
                new Document("range", Arrays.asList(-1, "current"))
                        .append("unit", "week"));
       
      Parameters:
      window - A Bson representing the required window.
      Returns:
      The constructed window.
    • documents

      public static Window documents(int lower, int upper)
      Creates a documents window whose bounds are determined by a number of documents before and after the current document.
      Parameters:
      lower - A value based on which the lower bound of the window is calculated.
      upper - A value based on which the upper bound of the window is calculated.
      Returns:
      The constructed documents window.
    • documents

      public static Window documents(Windows.Bound lower, int upper)
      Creates a documents window whose bounds are determined by a number of documents before and after the current document.
      Parameters:
      lower - A value based on which the lower bound of the window is calculated.
      upper - A value based on which the upper bound of the window is calculated.
      Returns:
      The constructed documents window.
    • documents

      public static Window documents(int lower, Windows.Bound upper)
      Creates a documents window whose bounds are determined by a number of documents before and after the current document.
      Parameters:
      lower - A value based on which the lower bound of the window is calculated.
      upper - A value based on which the upper bound of the window is calculated.
      Returns:
      The constructed documents window.
    • documents

      public static Window documents(Windows.Bound lower, Windows.Bound upper)
      Creates a documents window whose bounds are determined by a number of documents before and after the current document.
      Parameters:
      lower - A value based on which the lower bound of the window is calculated.
      upper - A value based on which the upper bound of the window is calculated.
      Returns:
      The constructed documents window.
    • range

      public static Window range(long lower, long upper)
      Creates a dynamically-sized range window whose bounds are determined by a range of possible values around the value of the sortBy field in the current document.
      Parameters:
      lower - A value based on which the lower bound of the window is calculated.
      upper - A value based on which the upper bound of the window is calculated.
      Returns:
      The constructed range window.
    • range

      public static Window range(double lower, double upper)
      Creates a dynamically-sized range window whose bounds are determined by a range of possible values around the value of the sortBy field in the current document.
      Parameters:
      lower - A value based on which the lower bound of the window is calculated.
      upper - A value based on which the upper bound of the window is calculated.
      Returns:
      The constructed range window.
    • range

      public static Window range(Decimal128 lower, Decimal128 upper)
      Creates a dynamically-sized range window whose bounds are determined by a range of possible values around the value of the sortBy field in the current document.
      Parameters:
      lower - A value based on which the lower bound of the window is calculated.
      upper - A value based on which the upper bound of the window is calculated.
      Returns:
      The constructed range window.
    • range

      public static Window range(Windows.Bound lower, long upper)
      Creates a dynamically-sized range window whose bounds are determined by a range of possible values around the value of the sortBy field in the current document.
      Parameters:
      lower - A value based on which the lower bound of the window is calculated.
      upper - A value based on which the upper bound of the window is calculated.
      Returns:
      The constructed range window.
    • range

      public static Window range(Windows.Bound lower, double upper)
      Creates a dynamically-sized range window whose bounds are determined by a range of possible values around the value of the sortBy field in the current document.
      Parameters:
      lower - A value based on which the lower bound of the window is calculated.
      upper - A value based on which the upper bound of the window is calculated.
      Returns:
      The constructed range window.
    • range

      public static Window range(Windows.Bound lower, Decimal128 upper)
      Creates a dynamically-sized range window whose bounds are determined by a range of possible values around the value of the sortBy field in the current document.
      Parameters:
      lower - A value based on which the lower bound of the window is calculated.
      upper - A value based on which the upper bound of the window is calculated.
      Returns:
      The constructed range window.
    • range

      public static Window range(long lower, Windows.Bound upper)
      Creates a dynamically-sized range window whose bounds are determined by a range of possible values around the value of the sortBy field in the current document.
      Parameters:
      lower - A value based on which the lower bound of the window is calculated.
      upper - A value based on which the upper bound of the window is calculated.
      Returns:
      The constructed range window.
    • range

      public static Window range(double lower, Windows.Bound upper)
      Creates a dynamically-sized range window whose bounds are determined by a range of possible values around the value of the sortBy field in the current document.
      Parameters:
      lower - A value based on which the lower bound of the window is calculated.
      upper - A value based on which the upper bound of the window is calculated.
      Returns:
      The constructed range window.
    • range

      public static Window range(Decimal128 lower, Windows.Bound upper)
      Creates a dynamically-sized range window whose bounds are determined by a range of possible values around the value of the sortBy field in the current document.
      Parameters:
      lower - A value based on which the lower bound of the window is calculated.
      upper - A value based on which the upper bound of the window is calculated.
      Returns:
      The constructed range window.
    • timeRange

      public static Window timeRange(long lower, long upper, MongoTimeUnit unit)
      Creates a dynamically-sized range window whose bounds are determined by a range of possible values around the BSON Date value of the sortBy field in the current document.
      Parameters:
      lower - A value based on which the lower bound of the window is calculated.
      upper - A value based on which the upper bound of the window is calculated.
      unit - A time unit in which lower and upper are specified.
      Returns:
      The constructed range window.
    • timeRange

      public static Window timeRange(Windows.Bound lower, long upper, MongoTimeUnit unit)
      Creates a dynamically-sized range window whose bounds are determined by a range of possible values around the BSON Date value of the sortBy field in the current document.
      Parameters:
      lower - A value based on which the lower bound of the window is calculated.
      upper - A value based on which the upper bound of the window is calculated.
      unit - A time unit in which upper is specified.
      Returns:
      The constructed range window.
    • timeRange

      public static Window timeRange(long lower, MongoTimeUnit unit, Windows.Bound upper)
      Creates a dynamically-sized range window whose bounds are determined by a range of possible values around the BSON Date value of the sortBy field in the current document.
      Parameters:
      lower - A value based on which the lower bound of the window is calculated.
      unit - A time unit in which lower is specified.
      upper - A value based on which the upper bound of the window is calculated.
      Returns:
      The constructed range window.