[GH-ISSUE #177] bug: MongoDB Adapter does not use plural collection name by default #8160

Closed
opened 2026-04-13 03:15:19 -05:00 by GiteaMirror · 3 comments
Owner

Originally created by @Iulian-Dragomirescu on GitHub (Oct 15, 2024).
Original GitHub issue: https://github.com/better-auth/better-auth/issues/177

I encountered an issue with the Mongoose adapter where it does not automatically use the plural form of the collection name. When I try to access the Account model using await Account.findOne(), I receive an empty array

const accountSchema = new mongoose.Schema({
  id: { type: String, required: true, unique: true },
  userId: { type: String, required: true, ref: "User" },
  accountId: { type: String, required: true },
  providerId: { type: String, required: true },
  accessToken: { type: String },
  refreshToken: { type: String },
  expiresAt: { type: Date },
  password: { type: String },
});

export const Account = mongoose.model("Account", accountSchema);

Attempt to fetch an account:

const account = await Account.findOne();

I receive an empty array when accessing await Account.findOne()

When using the following configuration with BetterAuth:

const client = mongoose.connection

export const auth = betterAuth({
  database: mongodbAdapter(client),
  emailAndPassword: {
    enabled: true,
  },
  user: {
    modelName: "users",
  },
  session: {
    modelName: "sessions",
  },
  account: {
    modelName: "accounts",
  },
  verification: {
    modelName: "verifications",
  },
});

The problem is resolved, but it should ideally work without the need to specify the model name in plural form

I believe the adapter should default to using the plural form of the model name without requiring explicit configuration for the collection name

Originally created by @Iulian-Dragomirescu on GitHub (Oct 15, 2024). Original GitHub issue: https://github.com/better-auth/better-auth/issues/177 I encountered an issue with the Mongoose adapter where it does not automatically use the plural form of the collection name. When I try to access the Account model using `await Account.findOne()`, I receive an empty array ``` const accountSchema = new mongoose.Schema({ id: { type: String, required: true, unique: true }, userId: { type: String, required: true, ref: "User" }, accountId: { type: String, required: true }, providerId: { type: String, required: true }, accessToken: { type: String }, refreshToken: { type: String }, expiresAt: { type: Date }, password: { type: String }, }); export const Account = mongoose.model("Account", accountSchema); ``` Attempt to fetch an account: ``` const account = await Account.findOne(); ``` I receive an empty array when accessing `await Account.findOne()` When using the following configuration with BetterAuth: ``` const client = mongoose.connection export const auth = betterAuth({ database: mongodbAdapter(client), emailAndPassword: { enabled: true, }, user: { modelName: "users", }, session: { modelName: "sessions", }, account: { modelName: "accounts", }, verification: { modelName: "verifications", }, }); ``` The problem is resolved, but it should ideally work without the need to specify the model name in plural form I believe the adapter should default to using the plural form of the model name without requiring explicit configuration for the collection name
GiteaMirror added the locked label 2026-04-13 03:15:19 -05:00
Author
Owner

@Bekacru commented on GitHub (Oct 16, 2024):

does mongo always use plural form collection names?

<!-- gh-comment-id:2417326864 --> @Bekacru commented on GitHub (Oct 16, 2024): does mongo always use plural form collection names?
Author
Owner

@Iulian-Dragomirescu commented on GitHub (Oct 16, 2024):

in mongoose yes: The first argument is the singular name of the collection your model is for. Mongoose automatically looks for the plural, lowercased version of your model name. Thus, for the example above, the model Tank is for the tanks collection in the database..
I personally did not use mongo directly, only through mongoose

<!-- gh-comment-id:2417569632 --> @Iulian-Dragomirescu commented on GitHub (Oct 16, 2024): in mongoose yes: `The first argument is the singular name of the collection your model is for. Mongoose automatically looks for the plural, lowercased version of your model name. Thus, for the example above, the model Tank is for the tanks collection in the database.`. I personally did not use mongo directly, only through mongoose
Author
Owner

@csyedbilal commented on GitHub (Oct 17, 2024):

Prisma Adapter is supported for MongoDB??

<!-- gh-comment-id:2420004302 --> @csyedbilal commented on GitHub (Oct 17, 2024): Prisma Adapter is supported for MongoDB??
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: github-starred/better-auth#8160