问题
I haven't been able to perform a complex query with an Any() inside an Any(), using MongoDB C# Driver..
I have this C# models (equivalent to the mongo database models):
[BsonCollection("alert_evaluations")]
public class AlertEvaluation : Document
{
public ICollection<EvaluationResult> EvaluationResults { get; set; }
public string EvaluationStatus { get; set; }
public DateTime EvaluatedAt { get; set; }
}
public class EvaluationResult
{
public ICollection<EvaluatedLabel> EvaluatedLabels { get; set; }
public double ResultNumber { get; set; }
}
public class EvaluatedLabel
{
public string LabelId { get; set; }
public string Value { get; set; }
}
And I get a list of the next model (as a parameter to filter):
public class EvaluatedLabelFilterDTO
{
public string LabelId { get; set; }
public string Value { get; set; }
}
What I would need to do is to filter AlertEvaluations by getting the ones that have EvaluationResults with EvaluatedLabels that match the same LabelId and Value of the list of EvaluatedLabelFilterDTO objects.
With LinQ I tried something like this:
FilterDefinitionBuilder<AlertEvaluation>.Where(x => x.EvaluationResults.Any(e => e.EvaluatedLabels.Any(z => evaluatedLabelsToFilter.Any(f => f.LabelId == z.LabelId && f.Value == z.Value))));
But of course, this is not supported by mongo..
I'm not really familiar with mongo queries.. Is there any way to do this as a BsonDocument filter query? (Or any other solution).
(The result has to be a FilterDefinition<>, not the list already).
I could really use some help with this. Thanks!
来源:https://stackoverflow.com/questions/65480659/how-to-run-complex-query-with-any-inside-any-mongodb-driver-c-sharp