I am having trouble querying a MongoDB database using a predicate which is dynamically generated.
The document structue i am trying to query on is:
{
Not sure it's a solution for mongodb, but could you try to use AsExpandable() coming from linqkit (made by the same guy as PredicateBuilder).
LinqKit (and more info about AsExpandable() and relation with PredicateBuilder) can be found here
Use the MongoDB Query builder instead of translating the predicate twice:
using MongoDB.Driver.Builders;
List<IMongoQuery> queries = new List<IMongoQuery>();
if (request.CultureId != null && request.CareerId != null) {
queries.Add(
Query<Person>.ElemMatch<IDependent>(p => p.Dependents,
q => Query.And(
q.EQ(x => x.CareerId, request.CareerId),
q.EQ(x => x.CultureId, request.CultureId))));
}
// ... etc
// Final Query:
var query = Query.And(queries);
I changed the query to use an $elemMatch, because I have the feeling you want to match both careerId and cultureId on the same array element. If that assumption is wrong, adjust the query accordingly.