Hello, first time here, coming in with a question since I’m just lost at this point. When working with this method:
public async Task<PaginatedData<DbUser>> GetPaginatedAsync(
int pageSize,
int page,
UserTypes userType,
string fullName
)
{
var countFacet = AggregateFacet.Create(
"count",
PipelineDefinition<DbUser, AggregateCountResult>.Create(
new[] { PipelineStageDefinitionBuilder.Count<DbUser>() }
)
); var facetFilter = Builders<DbUser>.Filter.Empty;
if (!string.IsNullOrWhiteSpace(fullName))
{
var regexPattern = new BsonRegularExpression($"^{fullName}", "i");
facetFilter &= Builders<DbUser>.Filter.Regex("full_name", regexPattern);
} var dataFacet = AggregateFacet.Create(
"data",
PipelineDefinition<DbUser, DbUser>.Create(
new[]
{
// PipelineStageDefinitionBuilder.Match(facetFilter),
PipelineStageDefinitionBuilder.Sort(Builders<DbUser>.Sort.Ascending(x => x.Id)),
PipelineStageDefinitionBuilder.Skip<DbUser>((page - 1) * pageSize),
PipelineStageDefinitionBuilder.Limit<DbUser>(pageSize),
PipelineStageDefinitionBuilder.Project<DbUser, DbUser>(
Builders<DbUser>.Projection.Exclude("password")
),
}
)
); var filter =
userType == UserTypes.All
? Builders<DbUser>.Filter.Empty
: Builders<DbUser>.Filter.Eq(x => x.UserType, (int)userType);
filter &= Builders<DbUser>.Filter.Eq(x => x.IsDeleted, false); if (!string.IsNullOrWhiteSpace(fullName))
{
var regexPattern = new BsonRegularExpression($"^{fullName}", "i");
filter &= Builders<DbUser>.Filter.Regex("full_name", regexPattern);
} var aggregation = await databaseContext
.Users.Aggregate()
.Match(filter)
.Facet(countFacet, dataFacet)
.ToListAsync(); var count =
aggregation
.First()
.Facets.First(x => x.Name == "count")
.Output<AggregateCountResult>()
?.FirstOrDefault()
?.Count ?? 0; var totalPages = Math.Ceiling(count / (double)pageSize); var data = aggregation.First().Facets.First(x => x.Name == "data").Output<DbUser>(); var employeeData = data.Select(x => x); return new PaginatedData<DbUser>
{
CurrentPage = page,
PageSize = pageSize,
TotalPages = (int)totalPages,
TotalRecords = count,
Data = employeeData
};
}
The data facet returns 0 records.
Removing the regex filter returns records correctly. Another method that uses the same approach works fine:
public async Task<IEnumerable<DbUser>> SearchAheadUserAsync(UserTypes userType, string fullName)
{
var regexPattern = new BsonRegularExpression($"^{fullName}", "i");
var filter = Builders<DbUser>.Filter.Regex("full_name", regexPattern); filter &=
userType == UserTypes.All
? Builders<DbUser>.Filter.Empty
: Builders<DbUser>.Filter.Eq(x => x.UserType, (int)userType); var results = await databaseContext.Users.Find(filter).ToListAsync(); return results;
}
One thing that I did notice is that the count facet is working correctly, it counts the amount of expected records according to the criteria. But the data facet isn’t mapping the records accordingly.
Has anyone have any advice as to how to make this work? Thanks!