Код: Выделить всё
public class Notification
{
[BsonId]
[BsonRepresentation(BsonType.ObjectId)]
public string Id { get; set; }
[BsonElement("Text")]
public string Text { get; set; }
[BsonElement("Recipients")]
public List Recipients { get; set; }
[BsonElement("Read")]
public bool Read { get; set; }
}
public class RecipientsDocument
{
[BsonElement("Typer")]
public string Type { get; set; }
[BsonElement("Code")]
public string Code { get; set; }
[BsonElement("CodeId")]
public Guid? CodeId { get; set; }
}
Код: Выделить всё
public class NotificationQueryFilter
{
public string? CodeFilter { get; set; }
public TypeRecipientsEnum Type { get; set; }
public List CodeIds { get; set; }
}
В этом методе я ищу документы на основе фильтров
Код: Выделить всё
public async Task GetByCode(string code, int PageNumber, int PageSize, List codeIds = null)
{
var filter = BuildFilter(new NotificationQueryFilter()
{
CodeFilter = code,
Type = TypeRecipientsEnum.Chief,
CodeIds = codeIds?.ToList()
});
int skip = (PageNumber - 1) * PageSize;
var sortBuilder = Builders.Sort;
var sort = sortBuilder.Descending(nameof(Notification.DataInserimento));
var docs = await GetCollection().Find(filter).Sort(sort).Skip(skip).Limit(PageSize).ToListAsync();
return docs;
}
private static FilterDefinition BuildFilter(NotificheQueryFilter item)
{
var filterDefinition = Builders.Filter;
FilterDefinition filter = FilterDefinition.Empty;
// Code filter
if (!string.IsNullOrEmpty(item.CodeFilter))
{
filter &= filterDefinition.ElemMatch(x => x.Recipients, dest => dest.Code == item.CodeFilter);
}
// CodeIds Filter
if(item.CodeIds != null && item.CodeIds.Any())
{
filter &= filterDefinition.Where(x => x.Recipients.Any(d => item.CodeIds.Any(z=> z == d.CodeId))); ***
Подробнее здесь: [url]https://stackoverflow.com/questions/78681203/filter-in-mongodb-doesnt-work-as-i-expect[/url]