public void Use_a_time_range_window_with_a_negative_upper_bound_example_should_work() { RequireServer.Check().Supports(Feature.SetWindowFields); var collection = Setup(); var aggregate = collection .Aggregate() .SetWindowFields( partitionBy: x => x.State, sortBy: Builders <CakeSales> .Sort.Ascending(x => x.OrderDate), output: p => new { RecentOrders = p.Push(x => x.OrderDate, RangeWindow.Create(RangeWindow.Unbounded, RangeWindow.Months(-10))) }); var stages = Linq3TestHelpers.Translate(collection, aggregate); var expectedStages = new[] { @" { $setWindowFields : { partitionBy : '$State', sortBy : { OrderDate : 1 }, output : { RecentOrders : { $push : '$OrderDate', window : { range : ['unbounded', -10], unit : 'month' } } } } } " }; Linq3TestHelpers.AssertStages(stages, expectedStages); var results = aggregate.ToList(); results.Count.Should().Be(6); results[0].Should().Be("{ _id : 4, Type : 'strawberry', OrderDate : ISODate('2019-05-18T16:09:01Z'), State : 'CA', Price : 41.00, Quantity : 162, RecentOrders : [ ] }"); results[1].Should().Be("{ _id : 0, Type : 'chocolate', OrderDate : ISODate('2020-05-18T14:10:30Z'), State : 'CA', Price : 13.00, Quantity : 120, RecentOrders : [ISODate('2019-05-18T16:09:01Z')] }"); results[2].Should().Be("{ _id : 2, Type : 'vanilla', OrderDate : ISODate('2021-01-11T06:31:15Z'), State : 'CA', Price : 12.00, Quantity : 145, RecentOrders : [ISODate('2019-05-18T16:09:01Z')] }"); results[3].Should().Be("{ _id : 5, Type : 'strawberry', OrderDate : ISODate('2019-01-08T06:12:03Z'), State : 'WA', Price : 43.00, Quantity : 134, RecentOrders : [ ] }"); results[4].Should().Be("{ _id : 3, Type : 'vanilla', OrderDate : ISODate('2020-02-08T13:13:23Z'), State : 'WA', Price : 13.00, Quantity : 104, RecentOrders : [ISODate('2019-01-08T06:12:03Z')] }"); results[5].Should().Be("{ _id : 1, Type : 'chocolate', OrderDate : ISODate('2021-03-20T11:30:05Z'), State : 'WA', Price : 14.00, Quantity : 140, RecentOrders : [ISODate('2019-01-08T06:12:03Z'), ISODate('2020-02-08T13:13:23Z')] }"); }
public void Range_window_example_should_work() { RequireServer.Check().Supports(Feature.SetWindowFields); var collection = Setup(); var aggregate = collection .Aggregate() .SetWindowFields( partitionBy: x => x.State, sortBy: Builders <CakeSales> .Sort.Ascending(x => x.Price), output: p => new { QuantityFromSimilarOrders = p.Sum(x => x.Quantity, RangeWindow.Create(-10, 10)) }); var stages = Linq3TestHelpers.Translate(collection, aggregate); var expectedStages = new[] { @" { $setWindowFields : { partitionBy : '$State', sortBy : { Price : 1 }, output : { QuantityFromSimilarOrders : { $sum : '$Quantity', window : { range : [-10.0, 10.0] } } } } } " }; Linq3TestHelpers.AssertStages(stages, expectedStages); var results = aggregate.ToList(); results.Count.Should().Be(6); results[0].Should().Be("{ _id : 2, Type : 'vanilla', OrderDate : ISODate('2021-01-11T06:31:15Z'), State : 'CA', Price : 12.00, Quantity : 145, QuantityFromSimilarOrders : 265 }"); results[1].Should().Be("{ _id : 0, Type : 'chocolate', OrderDate : ISODate('2020-05-18T14:10:30Z'), State : 'CA', Price : 13.00, Quantity : 120, QuantityFromSimilarOrders : 265 }"); results[2].Should().Be("{ _id : 4, Type : 'strawberry', OrderDate : ISODate('2019-05-18T16:09:01Z'), State : 'CA', Price : 41.00, Quantity : 162, QuantityFromSimilarOrders : 162 }"); results[3].Should().Be("{ _id : 3, Type : 'vanilla', OrderDate : ISODate('2020-02-08T13:13:23Z'), State : 'WA', Price : 13.00, Quantity : 104, QuantityFromSimilarOrders : 244 }"); results[4].Should().Be("{ _id : 1, Type : 'chocolate', OrderDate : ISODate('2021-03-20T11:30:05Z'), State : 'WA', Price : 14.00, Quantity : 140, QuantityFromSimilarOrders : 244 }"); results[5].Should().Be("{ _id : 5, Type : 'strawberry', OrderDate : ISODate('2019-01-08T06:12:03Z'), State : 'WA', Price : 43.00, Quantity : 134, QuantityFromSimilarOrders : 134 }"); }