Skip to content

Commit

Permalink
aggregation: Fix empty aggregation cursor count
Browse files Browse the repository at this point in the history
  • Loading branch information
BenjD90 committed Oct 30, 2023
1 parent 84226e9 commit a806892
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 1 deletion.
3 changes: 2 additions & 1 deletion src/cursors/n9-aggregation-cursor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ export class N9AggregationCursor<E> extends N9AbstractCursor<E> {
},
])
.toArray();
return countResult[0].n;
if (countResult.length > 0) return countResult[0].n;
return 0; // empty cursor
}
}
7 changes: 7 additions & 0 deletions test/aggregation-cursor.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,13 @@ test('[Cursor] Check count function', async (t: ExecutionContext<ContextContent>
t.context.mongoClient.newAggregationBuilder().match(filterQuery).group({ _id: 1 }),
);
t.is(await cursor.count(), 1, 'cursor only contains the group result');

cursor = t.context.mongoClient.aggregateWithBuilder<SampleType>(
t.context.mongoClient
.newAggregationBuilder()
.match({ $and: [{ _id: { $eq: '1' } }, { _id: { $ne: '1' } }] }),
);
t.is(await cursor.count(), 0, 'empty cursor count is 0');
});

test('[Cursor] Check cursor clone function', async (t: ExecutionContext<ContextContent>) => {
Expand Down

0 comments on commit a806892

Please sign in to comment.