[PR #4986] [CLOSED] fix(mongodb-adapter): add where clause support to count method #13941

Closed
opened 2026-04-13 09:13:36 -05:00 by GiteaMirror · 0 comments
Owner

📋 Pull Request Information

Original PR: https://github.com/better-auth/better-auth/pull/4986
Author: @t-kirsch
Created: 9/30/2025
Status: Closed

Base: canaryHead: fix/mongodb-adapter-count-where-clause


📝 Commits (5)

  • 380656d fix(mongodb-adapter): add where clause support to count method
  • 22206b8 fix: correct indentation for biome linter
  • 4e8d3a5 test(adapters): add tests for count method
  • 945c03b fix(memory-adapter): correctly handle where clause in count method
  • 601364f Merge branch 'canary' into fix/mongodb-adapter-count-where-clause

📊 Changes

1 file changed (+61 additions, -0 deletions)

View changed files

📝 packages/better-auth/src/adapters/test.ts (+61 -0)

📄 Description

Problem

The count method in the MongoDB adapter was ignoring the where parameter, causing it to count all documents in the collection instead of filtering by the provided conditions.

This was particularly problematic when checking organization membership limits before accepting invitations - it would count members from all organizations instead of just the specific organization, leading to incorrect limit validation.

Solution

  • Added where parameter to the count method signature
  • Use convertWhereClause to build the MongoDB filter properly
  • Matches the implementation pattern of other methods (findOne, findMany, etc.)

Changes

  • Modified packages/better-auth/src/adapters/mongodb-adapter/mongodb-adapter.ts
  • Changed the count method to accept and use the where parameter

Impact

This fix ensures that organization membership limits are correctly enforced per organization when:

  • Accepting invitations
  • Adding new members
  • Any other operation that uses countMembers

Summary by cubic

Fixes the MongoDB adapter’s count method to respect the where clause, so counts are correctly filtered. This prevents overcounting and ensures accurate organization membership limit checks.

  • Bug Fixes
    • Added where to the count method signature.
    • Built the MongoDB filter with convertWhereClause.
    • Aligned count with findOne/findMany implementation patterns.

🔄 This issue represents a GitHub Pull Request. It cannot be merged through Gitea due to API limitations.

## 📋 Pull Request Information **Original PR:** https://github.com/better-auth/better-auth/pull/4986 **Author:** [@t-kirsch](https://github.com/t-kirsch) **Created:** 9/30/2025 **Status:** ❌ Closed **Base:** `canary` ← **Head:** `fix/mongodb-adapter-count-where-clause` --- ### 📝 Commits (5) - [`380656d`](https://github.com/better-auth/better-auth/commit/380656d64516dbb17dc9f0924c87f1118b38d74a) fix(mongodb-adapter): add where clause support to count method - [`22206b8`](https://github.com/better-auth/better-auth/commit/22206b80a0d0c8ad120fa0aa88fb8c86bc1957d3) fix: correct indentation for biome linter - [`4e8d3a5`](https://github.com/better-auth/better-auth/commit/4e8d3a59ddf027ebbb1b3a2a59f6237fb7602b56) test(adapters): add tests for count method - [`945c03b`](https://github.com/better-auth/better-auth/commit/945c03beb90584c88f565f2dd740799fde859bb4) fix(memory-adapter): correctly handle where clause in count method - [`601364f`](https://github.com/better-auth/better-auth/commit/601364fee494efc85ebb286393f75fc2c80534fe) Merge branch 'canary' into fix/mongodb-adapter-count-where-clause ### 📊 Changes **1 file changed** (+61 additions, -0 deletions) <details> <summary>View changed files</summary> 📝 `packages/better-auth/src/adapters/test.ts` (+61 -0) </details> ### 📄 Description ## Problem The `count` method in the MongoDB adapter was ignoring the `where` parameter, causing it to count all documents in the collection instead of filtering by the provided conditions. This was particularly problematic when checking organization membership limits before accepting invitations - it would count members from **all organizations** instead of just the specific organization, leading to incorrect limit validation. ## Solution - Added `where` parameter to the `count` method signature - Use `convertWhereClause` to build the MongoDB filter properly - Matches the implementation pattern of other methods (`findOne`, `findMany`, etc.) ## Changes - Modified `packages/better-auth/src/adapters/mongodb-adapter/mongodb-adapter.ts` - Changed the `count` method to accept and use the `where` parameter ## Impact This fix ensures that organization membership limits are correctly enforced per organization when: - Accepting invitations - Adding new members - Any other operation that uses `countMembers` <!-- This is an auto-generated description by cubic. --> --- ## Summary by cubic Fixes the MongoDB adapter’s count method to respect the where clause, so counts are correctly filtered. This prevents overcounting and ensures accurate organization membership limit checks. - **Bug Fixes** - Added where to the count method signature. - Built the MongoDB filter with convertWhereClause. - Aligned count with findOne/findMany implementation patterns. <!-- End of auto-generated description by cubic. --> --- <sub>🔄 This issue represents a GitHub Pull Request. It cannot be merged through Gitea due to API limitations.</sub>
GiteaMirror added the pull-request label 2026-04-13 09:13:36 -05:00
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: github-starred/better-auth#13941