I have a single MongoDB collection holding documents of three different classes (A,B,C) which all inherit from a common class D.
Using the official C# driver, I have inserted documents of all three types (A,B,C) and they all appear correctly with the _t discriminator, and in my code their class maps are registered.
If I issue a LINQ query such as the following (using VB):
dim Result = database.GetCollection("mycol").AsQueryable(Of C).Where(some where clause)
If I count the results of this, I am getting an error "Element 'an element name from class A' does not match any field or property of class C."
Isn't the discriminator meant to kick in here in the AsQueryable(Of C)
code? It appears that when I issue .Count
my Where clause, which is specific to elements of class C, is being applied to documents of A,B, and C.
I have tried adding .OfType(Of C)
with no effect, have tried converting to a List first with .ToList
, but I continue to get the same error. Any ideas?
As background, my client code will usually deal with objects of type D. A, B, C share a lot of common properties inherited from D which I want to put indexes on, hence I put them in a single collection. However, occassionally I need to directly reference an object of types A, B, or C in special circumstances.