We're bringing our API into the 21st century, and updating all of our queries to use the newer (2.4) Mongo C# driver. One of our queries has this filter:
Query.Or(
Query<UserPermission>.EQ(p => p.UserId, userId),
Query<GroupPermission>.In(p => p.GroupId, groupIds)
)
UserPermission
and GroupPermission
both inherit from Permission
. In the old driver, this worked fine, since the underlying code just built the filter document and Mongo went on its merry way fetching the data.
With the new driver, I've looked at something like this:
permissionsQueryBuilder.Or(
userPermissionsQueryBuilder.Eq(p => p.UserId, userId),
groupPermissionsQueryBuilder.In(p => p.GroupId, groupIds)
)
Unfortunately, it doesn't work because Or expects FilterDefinition<Permission>[]
and the two query elements return FilterDefinition<UserPermission>
and FilterDefinition<GroupPermission>
respectively.
What is the correct way of approaching this using the new Mongo driver? I've searched Google but the search results seem to largely relate to _t
and how to store derived types, as opposed to how to query them.
Thanks in advance.
Using the old driver, this wasn't possible, but it turns out I can simply do this with the new driver:
permissionsQueryBuilder.Or(
permissionsQueryBuilder.Eq(p => (p as UserPermission).UserId, userId),
permissionsQueryBuilder.In(p => (p as GroupPermission).GroupId, groupIds)
)
Mongo seems to correctly compose the query when I use the as operator, although presumably a standard cast would work equally well.