public void Aggregate_Group_with_expressions_should_work( [Values(LinqProvider.V2, LinqProvider.V3)] LinqProvider linqProvider) { var collection = GetCollection <BsonDocument>(linqProvider); var aggregate = collection.Aggregate() .Group(x => 1, x => new { Count = x.Count() }); var stages = Linq3TestHelpers.Translate(collection, aggregate); var expectedStages = linqProvider switch { LinqProvider.V2 => new[] { "{ $group : { _id : 1, Count : { $sum : 1 } } }" }, LinqProvider.V3 => new[] { "{ $group : { _id : 1, __agg0 : { $sum : 1 } } }", "{ $project : { Count : '$__agg0', _id : 0 } }" }, _ => throw new ArgumentException() }; Linq3TestHelpers.AssertStages(stages, expectedStages); }
public void Use_documents_window_to_obtain_cumulative_and_maximum_quantity_for_each_year_example_should_work() { RequireServer.Check().Supports(Feature.SetWindowFields); var collection = Setup(); var aggregate = collection .Aggregate() .SetWindowFields( partitionBy: x => x.OrderDate.Year, sortBy: Builders <CakeSales> .Sort.Ascending(x => x.OrderDate), output: p => new { CumulativeQuantityForYear = p.Sum(x => x.Quantity, DocumentsWindow.Create(DocumentsWindow.Unbounded, DocumentsWindow.Current)), MaximumQuantityForYear = p.Max(x => x.Quantity, DocumentsWindow.Create(DocumentsWindow.Unbounded, DocumentsWindow.Unbounded)), }); var stages = Linq3TestHelpers.Translate(collection, aggregate); var expectedStages = new[] { @" { $setWindowFields : { partitionBy : { $year : '$OrderDate' } , sortBy : { OrderDate : 1 }, output : { CumulativeQuantityForYear : { $sum : '$Quantity', window : { documents : ['unbounded', 'current'] } }, MaximumQuantityForYear : { $max : '$Quantity', window : { documents : ['unbounded', 'unbounded'] } } } } } " }; Linq3TestHelpers.AssertStages(stages, expectedStages); var results = aggregate.ToList(); results.Count.Should().Be(6); results[0].Should().BeEquivalentTo("{ _id : 5, Type : 'strawberry', OrderDate : ISODate('2019-01-08T06:12:03Z'), State : 'WA', Price : 43.00, Quantity : 134, CumulativeQuantityForYear : 134, MaximumQuantityForYear : 162 }"); results[1].Should().BeEquivalentTo("{ _id : 4, Type : 'strawberry', OrderDate : ISODate('2019-05-18T16:09:01Z'), State : 'CA', Price : 41.00, Quantity : 162, CumulativeQuantityForYear : 296, MaximumQuantityForYear : 162 }"); results[2].Should().BeEquivalentTo("{ _id : 3, Type : 'vanilla', OrderDate : ISODate('2020-02-08T13:13:23Z'), State : 'WA', Price : 13.00, Quantity : 104, CumulativeQuantityForYear : 104, MaximumQuantityForYear : 120 }"); results[3].Should().BeEquivalentTo("{ _id : 0, Type : 'chocolate', OrderDate : ISODate('2020-05-18T14:10:30Z'), State : 'CA', Price : 13.00, Quantity : 120, CumulativeQuantityForYear : 224, MaximumQuantityForYear : 120 }"); results[4].Should().BeEquivalentTo("{ _id : 2, Type : 'vanilla', OrderDate : ISODate('2021-01-11T06:31:15Z'), State : 'CA', Price : 12.00, Quantity : 145, CumulativeQuantityForYear : 145, MaximumQuantityForYear : 145 }"); results[5].Should().BeEquivalentTo("{ _id : 1, Type : 'chocolate', OrderDate : ISODate('2021-03-20T11:30:05Z'), State : 'WA', Price : 14.00, Quantity : 140, CumulativeQuantityForYear : 285, MaximumQuantityForYear : 145 }"); }
public void Select_with_nested_field_name_should_work() { var collection = GetCollection(); var queryable = collection.AsQueryable() .Select(x => new { Field = x["field1"]["field2"] }); var stages = Linq3TestHelpers.Translate(collection, queryable); var expectedStages = new[] { "{ $project : { Field : '$field1.field2', _id : 0 } }" }; Linq3TestHelpers.AssertStages(stages, expectedStages); }
public void Where_comparing_field_names_and_two_indexes_should_work() { var collection = GetCollection(); var queryable = collection.AsQueryable() .Where(x => x["field1"][2][3] == x["field4"][5][6]); var stages = Linq3TestHelpers.Translate(collection, queryable); var expectedStages = new[] { "{ $match : { $expr : { $eq : [{ $arrayElemAt : [{ $arrayElemAt : ['$field1', 2] }, 3] }, { $arrayElemAt : [{ $arrayElemAt : ['$field4', 5] }, 6] }] } } }" }; Linq3TestHelpers.AssertStages(stages, expectedStages); }
public void Where_comparing_two_nested_field_names_should_work() { var collection = GetCollection(); var queryable = collection.AsQueryable() .Where(x => x["field1"]["field2"] == x["field3"]["field4"]); var stages = Linq3TestHelpers.Translate(collection, queryable); var expectedStages = new[] { "{ $match : { $expr : { $eq : ['$field1.field2', '$field3.field4'] } } }" }; Linq3TestHelpers.AssertStages(stages, expectedStages); }
public void Ternary_operator_should_work_in_Where() { var collection = GetCollection(); var queryable = collection.AsQueryable() .Where(x => (x.A == 0 ? 42 : x.A) != 144); var stages = Linq3TestHelpers.Translate(collection, queryable); var expectedStages = new[] { "{ $match : { $expr : { $ne : [{ $cond : { if : { $eq : ['$A', 0 ] }, then : 42, else : '$A' } }, 144] } } }" }; Linq3TestHelpers.AssertStages(stages, expectedStages); }
public void Where_with_lambda_should_work() { var collection = GetCollection(); var queryable = collection.AsQueryable() .Where(x => x.X > 0); var stages = Linq3TestHelpers.Translate(collection, queryable); var expectedStages = new[] { "{ $match : { X : { $gt : 0 } } }" }; Linq3TestHelpers.AssertStages(stages, expectedStages); }
public void Select_with_field_name_and_two_indexes_should_work() { var collection = GetCollection(); var queryable = collection.AsQueryable() .Select(x => new { Field = x["field1"][2][3] }); var stages = Linq3TestHelpers.Translate(collection, queryable); var expectedStages = new[] { "{ $project : { Field : { $arrayElemAt : [{ $arrayElemAt : ['$field1', 2] } , 3] }, _id : 0 } }" }; Linq3TestHelpers.AssertStages(stages, expectedStages); }
public void Select_comparing_nested_field_name_to_constant_should_work() { var collection = GetCollection(); var queryable = collection.AsQueryable() .Select(x => new { IsZero = x["field1"]["field2"] == 0 }); var stages = Linq3TestHelpers.Translate(collection, queryable); var expectedStages = new[] { "{ $project : { IsZero : { $eq : ['$field1.field2', 0] }, _id : 0 } }" }; Linq3TestHelpers.AssertStages(stages, expectedStages); }
public void Ternary_operator_should_work_in_Select() { var collection = GetCollection(); var queryable = collection.AsQueryable() .Select(x => new { result = (x.A == 0 ? 42 : x.A) }); var stages = Linq3TestHelpers.Translate(collection, queryable); var expectedStages = new[] { "{ $project : { result : { $cond : { if : { $eq : ['$A', 0] }, then : 42, else : '$A' } }, _id : 0 } }" }; Linq3TestHelpers.AssertStages(stages, expectedStages); }
public void IsNullOrEmpty_with_null_coalescing_operator_in_aggregate_match_should_work() { var collection = GetCollection(); var subject = collection.Aggregate(); var aggregate = subject.Match(x => string.IsNullOrEmpty(x.S ?? "")); var stages = Linq3TestHelpers.Translate(collection, aggregate); var expectedStages = new[] { "{ $match : { $expr : { $in : [{ $ifNull : ['$S', ''] }, [null, '']] } } }" // requires use of $expr }; Linq3TestHelpers.AssertStages(stages, expectedStages); }
public void IsNullOrEmpty_in_aggregate_match_should_work() { var collection = GetCollection(); var subject = collection.Aggregate(); var aggregate = subject.Match(x => string.IsNullOrEmpty(x.S)); var stages = Linq3TestHelpers.Translate(collection, aggregate); var expectedStages = new[] { "{ $match : { S : { $in : [null, ''] } } }" }; Linq3TestHelpers.AssertStages(stages, expectedStages); }
public void IsNullOrEmpty_in_queryable_where_should_work() { var collection = GetCollection(); var subject = collection.AsQueryable(); var queryable = subject.Where(x => string.IsNullOrEmpty(x.S)); var stages = Linq3TestHelpers.Translate(collection, queryable); var expectedStages = new[] { "{ $match : { S : { $in : [null, ''] } } }" }; Linq3TestHelpers.AssertStages(stages, expectedStages); }
public void Null_coalescing_operator_in_queryable_select_should_work() { var collection = GetCollection(); var subject = collection.AsQueryable(); var queryable = subject.Select(x => x.S ?? ""); var stages = Linq3TestHelpers.Translate(collection, queryable); var expectedStages = new[] { "{ $project : { _v : { $ifNull : ['$S', ''] }, _id : 0 } }" }; Linq3TestHelpers.AssertStages(stages, expectedStages); }
public void Where_comparing_field_name_and_index_and_nested_field_name_to_constant_should_work() { var collection = GetCollection(); var queryable = collection.AsQueryable() .Where(x => x["field1"][2]["field3"] == 0); var stages = Linq3TestHelpers.Translate(collection, queryable); var expectedStages = new[] { "{ $match : { 'field1.2.field3' : 0 } }" }; Linq3TestHelpers.AssertStages(stages, expectedStages); }
public void Select_comparing_field_names_and_two_indexes_should_work() { var collection = GetCollection(); var queryable = collection.AsQueryable() .Select(x => new { AreEqual = x["field1"][2][3] == x["field4"][5][6] }); var stages = Linq3TestHelpers.Translate(collection, queryable); var expectedStages = new[] { "{ $project : { AreEqual : { $eq : [{ $arrayElemAt : [{ $arrayElemAt : ['$field1' , 2] } , 3] }, { $arrayElemAt : [{ $arrayElemAt : ['$field4' , 5] } , 6] }] }, _id : 0 } }" }; Linq3TestHelpers.AssertStages(stages, expectedStages); }
public void Select_comparing_two_nested_field_names_should_work() { var collection = GetCollection(); var queryable = collection.AsQueryable() .Select(x => new { AreEqual = x["field1"]["field2"] == x["field3"]["field4"] }); var stages = Linq3TestHelpers.Translate(collection, queryable); var expectedStages = new[] { "{ $project : { AreEqual : { $eq : ['$field1.field2', '$field3.field4'] }, _id : 0 } }" }; Linq3TestHelpers.AssertStages(stages, expectedStages); }
public void Select_comparing_field_name_and__two_indexes_to_constant_should_work() { var collection = GetCollection(); var queryable = collection.AsQueryable() .Select(x => new { IsZero = x["field1"][2][3] == 0 }); var stages = Linq3TestHelpers.Translate(collection, queryable); var expectedStages = new[] { "{ $project : { IsZero : { $eq : [{ $arrayElemAt : [{ $arrayElemAt : ['$field1', 2] }, 3] }, 0] }, _id : 0 } }" }; Linq3TestHelpers.AssertStages(stages, expectedStages); }
public void GroupBy_followed_by_select_should_translate_as_expected() { var collection = CreateCollection(); var subject = collection.AsQueryable(); var queryable = subject.GroupBy(c => c.A).Select(g => new { Result = g.Select(x => x) }); var stages = Linq3TestHelpers.Translate(collection, queryable); var expectedStages = new[] { "{ $group : { _id : '$A', __agg0 : { $push : '$$ROOT' } } }", "{ $project : { Result : '$__agg0', _id : 0 } }" }; Linq3TestHelpers.AssertStages(stages, expectedStages); }
public void Expression_using_ToUpperInvariant_should_work() { var collection = GetCollection(); var queryable = from product in collection.AsQueryable() select new { Result = product.FriendlyName.ToUpperInvariant() }; var stages = Linq3TestHelpers.Translate(collection, queryable); var expectedStages = new[] { "{ $project : { Result : { $toUpper : '$FriendlyName' }, _id : 0 } }" }; Linq3TestHelpers.AssertStages(stages, expectedStages); }
public void GroupBy_with_key_selector_and_result_selector_should_translate_as_expected() { var collection = CreateCollection(); var subject = collection.AsQueryable(); var queryable = subject.GroupBy(c => c.A, (k, g) => new { Result = g.First() }); var stages = Linq3TestHelpers.Translate(collection, queryable); var expectedStages = new[] { "{ $group : { _id : '$A', __agg0 : { $first : '$$ROOT' } } }", "{ $project : { Result : '$__agg0', _id : 0 } }" }; Linq3TestHelpers.AssertStages(stages, expectedStages); }
public void Aggregate_Group_with_projection_to_TNewResult_should_work( [Values(LinqProvider.V2, LinqProvider.V3)] LinqProvider linqProvider) { var collection = GetCollection <BsonDocument>(linqProvider); var aggregate = collection.Aggregate() .Group <Result>("{ _id : 1, Count : { $sum : 1 } }"); var stages = Linq3TestHelpers.Translate(collection, aggregate); var expectedStages = new[] { "{ $group : { _id : 1, Count : { $sum : 1 } } }" }; Linq3TestHelpers.AssertStages(stages, expectedStages); }
public void ToLower_with_Equals_should_work_in_Where() { var client = DriverTestConfiguration.Linq3Client; var database = client.GetDatabase("test"); var collection = database.GetCollection <C>("test"); var queryable = collection.AsQueryable() .Where(x => x.S.ToLower().Equals("abc")); var stages = Linq3TestHelpers.Translate(collection, queryable); var expectedStages = new[] { "{ $match : { S : /^abc$/is } }" }; Linq3TestHelpers.AssertStages(stages, expectedStages); }
public void Where_with_dynamically_created_predicate_should_work( [Values("x", null)] string parameterName) { var collection = GetCollection(); var predicate = MakePredicate(parameterName); var queryable = collection.AsQueryable() .Where(predicate); var stages = Linq3TestHelpers.Translate(collection, queryable); var expectedStages = new[] { "{ $match : { X : { $gt : 0 } } }" }; Linq3TestHelpers.AssertStages(stages, expectedStages); }
public void ToLower_with_Equals_should_work_in_Select() { var client = DriverTestConfiguration.Linq3Client; var database = client.GetDatabase("test"); var collection = database.GetCollection <C>("test"); var queryable = collection.AsQueryable() .Select(x => new { Result = x.S.ToLower().Equals("abc") }); var stages = Linq3TestHelpers.Translate(collection, queryable); var expectedStages = new[] { "{ $project : { Result : { $eq : [{ $toLower : '$S' }, 'abc'] }, _id : 0 } }" }; Linq3TestHelpers.AssertStages(stages, expectedStages); }
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 Use_documents_window_to_obtain_moving_average_quantity_for_each_year_example_should_work() { RequireServer.Check().Supports(Feature.SetWindowFields); var collection = Setup(); var aggregate = collection .Aggregate() .SetWindowFields( partitionBy: x => x.OrderDate.Year, sortBy: Builders <CakeSales> .Sort.Ascending(x => x.OrderDate), output: p => new { AverageQuantity = p.Average(x => x.Quantity, DocumentsWindow.Create(-1, 0)) }); var stages = Linq3TestHelpers.Translate(collection, aggregate); var expectedStages = new[] { @" { $setWindowFields : { partitionBy : { $year : '$OrderDate' } , sortBy : { OrderDate : 1 }, output : { AverageQuantity : { $avg : '$Quantity', window : { documents : [-1, 0] } } } } } " }; Linq3TestHelpers.AssertStages(stages, expectedStages); var results = aggregate.ToList(); results.Count.Should().Be(6); results[0].Should().Be("{ _id : 5, Type : 'strawberry', OrderDate : ISODate('2019-01-08T06:12:03Z'), State : 'WA', Price : 43.00, Quantity : 134, AverageQuantity : 134.0 }"); results[1].Should().Be("{ _id : 4, Type : 'strawberry', OrderDate : ISODate('2019-05-18T16:09:01Z'), State : 'CA', Price : 41.00, Quantity : 162, AverageQuantity : 148.0 }"); results[2].Should().Be("{ _id : 3, Type : 'vanilla', OrderDate : ISODate('2020-02-08T13:13:23Z'), State : 'WA', Price : 13.00, Quantity : 104, AverageQuantity : 104.0 }"); results[3].Should().Be("{ _id : 0, Type : 'chocolate', OrderDate : ISODate('2020-05-18T14:10:30Z'), State : 'CA', Price : 13.00, Quantity : 120, AverageQuantity : 112.0 }"); results[4].Should().Be("{ _id : 2, Type : 'vanilla', OrderDate : ISODate('2021-01-11T06:31:15Z'), State : 'CA', Price : 12.00, Quantity : 145, AverageQuantity : 145.0 }"); results[5].Should().Be("{ _id : 1, Type : 'chocolate', OrderDate : ISODate('2021-03-20T11:30:05Z'), State : 'WA', Price : 14.00, Quantity : 140, AverageQuantity : 142.5 }"); }
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 }"); }
public void ToUpperInvariant_with_Equals_should_work() { var collection = GetCollection(); var friendlyName = "Widget"; var queryable = from product in collection.AsQueryable() where product.FriendlyName.ToUpperInvariant().Equals(friendlyName.ToUpper()) select product; var stages = Linq3TestHelpers.Translate(collection, queryable); var expectedStages = new[] { "{ $match : { FriendlyName : /^WIDGET$/is } }" }; Linq3TestHelpers.AssertStages(stages, expectedStages); }
public void Filter_using_ToLower_with_equality_operator_should_work() { var collection = GetCollection(); var friendlyName = "Widget"; var queryable = from product in collection.AsQueryable() where product.FriendlyName.ToLower() == friendlyName.ToLower() select product; var stages = Linq3TestHelpers.Translate(collection, queryable); var expectedStages = new[] { "{ $match : { FriendlyName : /^widget$/is } }" }; Linq3TestHelpers.AssertStages(stages, expectedStages); }