[CSHARP-1925] Filters in subdocuments can't use fields from the parent document Created: 18/Feb/17  Updated: 05/Feb/22  Resolved: 05/Feb/22

Status: Closed
Project: C# Driver
Component/s: Linq
Affects Version/s: 2.4.1
Fix Version/s: 2.14.0

Type: Bug Priority: Major - P3
Reporter: Daniel Rusznyak Assignee: James Kovacs
Resolution: Done Votes: 0
Labels: None
Remaining Estimate: Not Specified
Time Spent: Not Specified
Original Estimate: Not Specified
Environment:

ASP.NET Core


Epic Link: CSHARP-3615

 Description   

Using a filter via .Where() inside a Select uses the subdocument's value instead of the parent document's.
i.e:

// Some comments here
select new
{
    Prev = prev.Where(x => x.LastActionTime < visit.FirstActionTime && x.VisitorId != null)
} into r

Expected result:

"Prev": {
                "$filter": {
                    "input": "$prev",
                    "as": "x",
                    "cond": {
                        "$and": [
                            {
                                "$lt": [
                                    "$$x.LastActionTime",
                                    "$FirstActionTime"
                                ]
                            },
                            {
                                "$ne": [
                                    "$$x.VisitorId",
                                    null
                                ]
                            }
                        ]
                    }
                }
            },

Actual result:

"Prev": {
                "$filter": {
                    "input": "$prev",
                    "as": "x",
                    "cond": {
                        "$and": [
                            {
                                "$lt": [
                                    "$$x.LastActionTime",
                                    "$$x.FirstActionTime"
                                ]
                            },
                            {
                                "$ne": [
                                    "$$x.VisitorId",
                                    null
                                ]
                            }
                        ]
                    }
                }
            },



 Comments   
Comment by James Kovacs [ 05/Feb/22 ]

This issue has been fixed in the new LINQ provider (known as LINQ3), which is included 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 Daniel Rusznyak [ 18/Feb/17 ]

As an additional note, both the main document and the child document has the same type and come from the same collection.

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