In the process of creating a filtering feature in my mongoDB i have created the following find query using the mongoDB driver for c#.
return await DefaultCollection
.Find(c => c.vintageInfo.IsVintage
&& c.IsPublished
&& c.AverageRating >= lowerRating
&& filterArray1.Contains(c.filterSearchParameters.Dosage))
.Skip(page * pageSize)
.Limit(pageSize)
.ToListAsync();
This is working perfect and it's all great!
Now as we see in the example above i can check if the provided filterArray1 contains the document attribute c.filterSearchParameters.Dosage. Now the problem arises cause, the document further contains a list as follows c.filterSearchParameters.Styles. What i would like to be able to do is check if the list c.filterSearchParameters.Styles contains any value matching filterArray2.
Edit
As per request:
filterSearchParameters looks like:
public class FilterSearchParameters
{
public string Dosage { get; set; }
public List<string> Style { get; set; }
public List<string> Character { get; set; }
}
and the filterArray1 and 2 is just an ordinary:
List<string> filterArray1
List<string> filterArray2
To do this i tried:
return await DefaultCollection
.Find(c => c.vintageInfo.IsVintage
&& c.IsPublished
&& c.AverageRating >= lowerRating
&& filterArray1.Contains(c.filterSearchParameters.Dosage)
&& filterArray2.Any(x => c.filterSearchParameters.Style.Any(y => y.Equals(x))))
.Skip(page * pageSize)
.Limit(pageSize)
.ToListAsync();
Adding the line:
filterArray2.Any(x => c.filterSearchParameters.Style.Any(y => y.Equals(x)))
However this throws Unsopported filter exception. So i tried:
return await DefaultCollection
.Find(c => c.vintageInfo.IsVintage
&& c.IsPublished
&& c.AverageRating >= lowerRating
&& filterArray1.Contains(c.filterSearchParameters.Dosage)
&& c.filterSearchParameters.Style.Intersect(filterArray2).Any())
.Skip(page * pageSize)
.Limit(pageSize)
.ToListAsync();
Adding the line :
c.filterSearchParameters.Style.Intersect(filterArray2).Any()
However the error persisted...
Lastly ive found ->
Builders<Entity>.Filter.ElemMatch(
x => x.filterSearchParameters.Style,
s => filterArray2.Contains(s))
But havn't been able to incorporate it and try it out with the other filters.
Does anyone know how to solve this problem, or any knowledge that could point me in the direction of some documentation i could read, or say if i'm already looking in the wrong place... Any help is appreaciated, thanks in advance.
所有评论(0)