[CSHARP-4616] Linqprovider V3 IMongoCollection<T>.AsQueryable().OfType<T> does not match derived types Created: 14/Apr/23  Updated: 28/Oct/23  Resolved: 05/May/23

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

Type: Bug Priority: Unknown
Reporter: David Ferrante Assignee: Oleksandr Poliakov
Resolution: Fixed Votes: 0
Labels: triage
Remaining Estimate: Not Specified
Time Spent: Not Specified
Original Estimate: Not Specified

Backwards Compatibility: Fully Compatible
Documentation Changes: Not Needed
Documentation Changes Summary:

1. What would you like to communicate to the user about this feature?
2. Would you like the user to see examples of the syntax and/or executable code and its output?
3. Which versions of the driver/connector does this apply to?


 Description   

Summary

When using Linqprovider V3 IMongoCollection<T>.AsQueryable().OfType<T> does not match derived types.

Query Produced in V3

{
    basis_dev.jobs.Aggregate([{
                "$match": {
                    _"t": ["Entity", "Job"]
                }
            }, {
                "$match": {
                    "ParentJobId": {
                        "$in": [ObjectId("6438717105606533eb0d2bfe"), ObjectId("6438717105606533eb0d2bff"), ObjectId("6438717105606533eb0d2c00"), ObjectId("6438717105606533eb0d2c01"), ObjectId("6438717105606533eb0d2c02"), ObjectId("6438717105606533eb0d2c03")]
                    }
                }
            }
        ])
}

 

Query Produced in V2

{
    aggregate([{
                "$match": {
                    _"t": "Job"
                }
            }, {
                "$match": {
                    "ParentJobId": {
                        "$in": [ObjectId("6438717105606533eb0d2bfe"), ObjectId("6438717105606533eb0d2bff"), ObjectId("6438717105606533eb0d2c00"), ObjectId("6438717105606533eb0d2c01"), ObjectId("6438717105606533eb0d2c02"), ObjectId("6438717105606533eb0d2c03")]
                    }
                }
            }
        ])
}

Please provide the version of the driver. If applicable, please provide the MongoDB server version and topology (standalone, replica set, or sharded cluster).

Version 2.18.0 and version 2.19.1 were tested and both result in the same query. 

Mongo Atlas 6.0.5

How to Reproduce

[JsonKnownType]
[BsonKnownType]
public abstract class Entity

{     [BsonId]     [JsonProperty("id")]     [BsonRepresentation(BsonType.ObjectId)]     public string Id \{ get; set; }

}

Entity is set as root and as KnownType

[Table(Constants.MongoDb.CollectionNames.Jobs)]
public abstract class Job : Entity

{         [BsonRepresentation(BsonType.ObjectId)]     public string ParentJobId \{ get; set; }

}

public class SampleJob : Job

{     public string SampleJobName \{ get; set; }

    public TimeSpan? TimeSpan { get; set; }
}

 

Query is 

var parentJobIds = new List<string> { "6438717105606533eb0d2bfe" };

IMongoCollection<Job>.AsQueryable().OfType<Job>().Where(x=> parentJobIds.Contains(x.ParentJobId));

Additional Background

The expected result of the query should return all derived Job types, SampleJob being one of them, but it does not. 

This works fine in LinqProviderV2 and is preventing us from moving to V3. 



 Comments   
Comment by Githook User [ 18/May/23 ]

Author:

{'name': 'Oleksandr Poliakov', 'email': '31327136+sanych-sun@users.noreply.github.com', 'username': 'sanych-sun'}

Message: CSHARP-4616: OfType should match derived types (#1075)
Branch: v2.19.x
https://github.com/mongodb/mongo-csharp-driver/commit/9d8787127a2000dbeee83ee03352a5fc0b4bc876

Comment by Githook User [ 05/May/23 ]

Author:

{'name': 'Oleksandr Poliakov', 'email': '31327136+sanych-sun@users.noreply.github.com', 'username': 'sanych-sun'}

Message: CSHARP-4616: OfType should match derived types (#1075)
Branch: master
https://github.com/mongodb/mongo-csharp-driver/commit/71f42618e719caeb1c83299d6705115bec3e2643

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