[CSHARP-4328] First support for array first element match Created: 23/Sep/22  Updated: 27/Oct/23  Resolved: 29/Sep/22

Status: Closed
Project: C# Driver
Component/s: None
Affects Version/s: None
Fix Version/s: None

Type: Improvement Priority: Unknown
Reporter: Weihan Li Assignee: Robert Stam
Resolution: Works as Designed Votes: 0
Labels: None
Remaining Estimate: Not Specified
Time Spent: Not Specified
Original Estimate: Not Specified


 Description   

 

        var filterBuilder = Builders<SubscriptionRepositorySubscriptionModel>.Filter;
        var statusFilter = filterBuilder.Where(x => x.SubscriptionStatus == (byte)SubscriptionStatus.Active);
        var dateFilter = filterBuilder.Where(x => x.UpcomingOrders.First().NextTargetDeliveryDate >= DateTime.UtcNow 
                                                  && x.UpcomingOrders.First().NextTargetDeliveryDate <= targetDeliveryDate);
        var filter = filterBuilder.And(statusFilter, dateFilter);
        if (!string.IsNullOrEmpty(country))
        {
            filter = filterBuilder.And(filter, filterBuilder.Where(x => x.ShippingAddress.CountryCode == country));
        }
        var result = await _subscriptionCollection.Distinct(x => x.CustomerId, filter).ToListAsync(); 

Exception:

System.InvalidOperationException: First({document}{UpcomingOrders}.Select({document}{NextTargetDeliveryDate})) is not supported.\r\n   at MongoDB.Driver.Linq.Linq2Implementation.Translators.PredicateTranslator.GetFieldExpression(Expression expression)\r\n   at MongoDB.Driver.Linq.Linq2Implementation.Translators.PredicateTranslator.TranslateComparison(Expression variableExpression, ExpressionType operatorType, ConstantExpression constantExpression)\r\n   at MongoDB.Driver.Linq.Linq2Implementation.Translators.PredicateTranslator.Translate(Expression node)\r\n   at MongoDB.Driver.Linq.Linq2Implementation.Translators.PredicateTranslator.TranslateAndAlso(BinaryExpression node)\r\n   at MongoDB.Driver.Linq.Linq2Implementation.Translators.PredicateTranslator.Translate(Expression node)\r\n   at MongoDB.Driver.Linq.Linq2Implementation.Translators.PredicateTranslator.Translate(Expression node, IBsonSerializerRegistry serializerRegistry)\r\n   at MongoDB.Driver.Linq.Linq2Implementation.Translators.PredicateTranslator.Translate[TDocument](Expression`1 predicate, IBsonSerializer`1 parameterSerializer, IBsonSerializerRegistry serializerRegistry)\r\n  

 



 Comments   
Comment by Weihan Li [ 09/Jun/23 ]

I'm not using the linq3 provider, is it possible to support via linq2 provider

when I upgrade from 2.18.0 to 2.19.2, this works, while some other working expressions previously reported errors

Comment by Robert Stam [ 28/Sep/22 ]

This issue has been fixed in the new LINQ provider (known as LINQ3), which was introduced in the 2.14 release.

Configure your MongoClientSettings to use LinqProvider.V3 if you want to use this functionality.

To configure a client to use the LINQ3 provider use code like the following

var connectionString = "mongodb://localhost";
var clientSettings = MongoClientSettings.FromConnectionString(connectionString);
clientSettings.LinqProvider = LinqProvider.V3;
var client = new MongoClient(clientSettings);

Comment by Githook User [ 28/Sep/22 ]

Author:

{'name': 'rstam', 'email': 'robert@robertstam.org', 'username': 'rstam'}

Message: CSHARP-4328: Code review changes.
Branch: master
https://github.com/mongodb/mongo-csharp-driver/commit/8db33637a81cac1bbc5e90ce8014bc9bb843d123

Comment by Githook User [ 28/Sep/22 ]

Author:

{'name': 'rstam', 'email': 'robert@robertstam.org', 'username': 'rstam'}

Message: CSHARP-4328: Verify First in filter works in LINQ3.
Branch: master
https://github.com/mongodb/mongo-csharp-driver/commit/dd0cae329bb73fe89fbccb8aae8ab939794a6f17

Comment by Dmitry Lukyanov (Inactive) [ 23/Sep/22 ]

Thanks weihanli@outlook.com for your report, we will look at this and come back to you

Generated at Wed Feb 07 21:47:54 UTC 2024 using Jira 9.7.1#970001-sha1:2222b88b221c4928ef0de3161136cc90c8356a66.