Skip to content

Commit

Permalink
Use the original base query for counting total items, not the one wit…
Browse files Browse the repository at this point in the history
…h paging on it
  • Loading branch information
einari committed Jul 23, 2024
1 parent bf2a1c8 commit 35e9999
Showing 1 changed file with 14 additions and 11 deletions.
25 changes: 14 additions & 11 deletions Source/DotNET/MongoDB/MongoCollectionExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -207,12 +207,13 @@ async Task Watch()
{
try
{
var response = findCall();
await UpdateTotalItems(queryContext, response);
var query = findCall();
await UpdateTotalItems(queryContext, query);
var baseQuery = query;

response = AddSorting(queryContext, response);
response = AddPaging(queryContext, response);
documents = response.ToList();
query = AddSorting(queryContext, query);
query = AddPaging(queryContext, query);
documents = query.ToList();
onNext(documents, subject);

cursor = await collection.WatchAsync(pipeline, options);
Expand All @@ -234,7 +235,8 @@ await cursor.ForEachAsync(
onNext,
changeDocument,
invalidateFindOnAddOrDelete,
response,
baseQuery,
query,
documents,
subject,
idProperty);
Expand Down Expand Up @@ -262,17 +264,18 @@ await cursor.ForEachAsync(
return subject;
}

static async Task UpdateTotalItems<TDocument>(QueryContext queryContext, IFindFluent<TDocument, TDocument> response)
static async Task UpdateTotalItems<TDocument>(QueryContext queryContext, IFindFluent<TDocument, TDocument> query)
{
queryContext.TotalItems = await response.CountDocumentsAsync();
queryContext.TotalItems = await query.CountDocumentsAsync();
}

static async Task<List<TDocument>> HandleChange<TDocument, TResult>(
QueryContext queryContext,
Action<IEnumerable<TDocument>, ISubject<TResult>> onNext,
ChangeStreamDocument<TDocument> changeDocument,
bool invalidateFindOnAddOrDelete,
IFindFluent<TDocument, TDocument> response,
IFindFluent<TDocument, TDocument> baseQuery,
IFindFluent<TDocument, TDocument> query,
List<TDocument> documents,
ISubject<TResult> subject,
PropertyInfo idProperty)
Expand Down Expand Up @@ -307,8 +310,8 @@ static async Task<List<TDocument>> HandleChange<TDocument, TResult>(

if (invalidateFindOnAddOrDelete)
{
await UpdateTotalItems(queryContext, response);
documents = await response.ToListAsync();
await UpdateTotalItems(queryContext, baseQuery);
documents = await query.ToListAsync();
}
}

Expand Down

0 comments on commit 35e9999

Please sign in to comment.