[CSHARP-1668] Allow FilterDefinition OfType to allow for querying of inherited types with type safety in builder Created: 12/May/16 Updated: 05/Apr/19 Resolved: 03/Nov/17 |
|
| Status: | Closed |
| Project: | C# Driver |
| Component/s: | Read Operations |
| Affects Version/s: | 2.2.3 |
| Fix Version/s: | None |
| Type: | Task | Priority: | Major - P3 |
| Reporter: | Chad Kreimendahl | Assignee: | Robert Stam |
| Resolution: | Done | Votes: | 2 |
| Labels: | question | ||
| Remaining Estimate: | Not Specified | ||
| Time Spent: | Not Specified | ||
| Original Estimate: | Not Specified | ||
| Issue Links: |
|
||||||||
| Description |
|
The ability to use builders for typesafety with expressions is excellent, especially given our abundant use of the BsonClassMap to map field names, and occasionally change them. We would like the ability to do the following: Builders<RootType>.Filter.OfType<SubType>().ElemMatch(c => c.SomeThing, Builders<SubType>.Filter.Eq(c => c.SubTypeBoolean, true)) |
| Comments |
| Comment by Robert Stam [ 03/Nov/17 ] | ||||||||||||||||||||||||||
|
Suppose you had a collection of documents of type Root, with subclasses S and T.
If I'm understanding correctly what you want to do, you want a single query that checks some properties only available in some subclasses. Let's assume you want to return all documents that match any of the following: 1. All documents where TV == 1 This can be written as follows:
which results in the following query being sent to the server:
| ||||||||||||||||||||||||||
| Comment by Chad Kreimendahl [ 25/Aug/16 ] | ||||||||||||||||||||||||||
|
Our main issue is that second comment I posted. Though an .Or() would likely be more appropriate given how I showed the two Eqs. Basically we frequently need to pull back a bunch of records where some overall setting is there, or some subtype setting also exists. | ||||||||||||||||||||||||||
| Comment by Craig Wilson [ 10/Aug/16 ] | ||||||||||||||||||||||||||
|
Chad, I was about to start adding these and we already have OfType on FilterDefintions. See here: https://github.com/mongodb/mongo-csharp-driver/blob/master/src/MongoDB.Driver/FilterDefinitionBuilder.cs#L966-L1054. Is there something missing that doesn't satisfy your needs. We can't fix the covariance problem as that is a compiler thing. Craig | ||||||||||||||||||||||||||
| Comment by Chad Kreimendahl [ 27/May/16 ] | ||||||||||||||||||||||||||
|
Another good example of this would be
| ||||||||||||||||||||||||||
| Comment by Chad Kreimendahl [ 19/May/16 ] | ||||||||||||||||||||||||||
|
It would be really interesting if you could somehow get the following done. I'm having a painful time wrapping my head around how this would be written to work in the driver, as there doesn't seem to be a way in C# to accept covariants of generics:
|