MongoDB not equal (ne) filter operator not working in listUsers() #2086

Closed
opened 2026-03-13 09:26:03 -05:00 by GiteaMirror · 0 comments
Owner

Originally created by @pneumaticos on GitHub (Oct 7, 2025).

I have resulted to using eq filter then filtering the data received to exclude the logged in user. Using id instead of _id seemed not to work

Is this suited for github?

  • Yes, this is suited for github

To Reproduce

On the backend, try accessing the users:

import { auth } from "@/lib/auth/auth";
import { headers } from "next/headers";


export default async function StaffManagementPage() {
  const session = await auth.api.getSession({
    headers: await headers(),
  });

  const user = session.user;

  const users = await auth.api.listUsers({
    headers: await headers(),
    query: {
      limit: 10,
      filterField: "_id",
      filterOperator: "ne",
      filterValue: user.id,
    },
  });

  console.log("Current user:", user.id);
  console.log("Returned users:", users);
}

Current vs. Expected behavior

The currently logged in user should be excluded but he isn't.

What version of Better Auth are you using?

1.3.27

System info

{
  "system": {
    "platform": "win32",
    "arch": "x64",
    "version": "Windows 10 Pro",
    "release": "10.0.19045",
    "cpuCount": 8,
    "cpuModel": "Intel(R) Core(TM) i5-8365U CPU @ 1.60GHz", 
    "totalMemory": "15.82 GB",
    "freeMemory": "2.90 GB"
  },
  "node": {
    "version": "v22.19.0",
    "env": "development"
  },
  "packageManager": {
    "name": "npm",
    "version": "11.5.2"
  },
  "frameworks": [
    {
      "name": "next",
      "version": "15.5.2"
    },
    {
      "name": "react",
      "version": "19.1.0"
    }
  ],
  "databases": [
    {
      "name": "mongodb",
      "version": "6.18.0"
    }
  ],
  "betterAuth": {
    "version": "^1.3.27",
    "config": null
  }
}

Which area(s) are affected? (Select all that apply)

Backend

Auth config (if applicable)

import { betterAuth } from "better-auth";
import { MongoClient } from "mongodb";
import { mongodbAdapter } from "better-auth/adapters/mongodb";
import { authClient } from "./auth-client";
import { logSuccess } from "../logger";
import { nextCookies } from "better-auth/next-js";
import { admin as adminPlugin, multiSession } from "better-auth/plugins";
import { ac, admin, staff } from "./permissions";

const client = new MongoClient(process.env.MONGODB_URI_LINK!);

const db = client.db();

client.on("open", () => {
  logSuccess("✅ MongoDB connection opened");
});

export const auth = betterAuth({
  emailAndPassword: {
    enabled: true,
    async sendResetPassword(data) {
      // Send an email to the user with a link to reset their password
      console.log(data);
    },
  },
  database: mongodbAdapter(db, {
    client,
  }),
  user: {
    additionalFields: {
      role: {
        type: ["staff", "admin"],
        required: true,
        defaultValue: "admin",
      },
      onboarding: {
        type: "boolean",
        required: true,
        defaultValue: false,
      },
      restaurantId: {
        type: "string",
        required: false,
      },
    },
  },
  trustedOrigins: ["http://192.168.100.21:3000"],
  // session: {
  //   cookieCache: {
  //     enabled: true,
  //     maxAge: 20 * 60,
  //   },
  // },
  plugins: [
    adminPlugin({
      ac,
      roles: {
        staff,
        admin,
      },
      defaultRole: "admin",
    }),
    multiSession({
      maximumSessions: 1,
    }),
    nextCookies(),
  ],
});

export type Session = typeof authClient.$Infer.Session;

Additional context

  1. Tried both filterField: "id" and filterField: "_id" - only _id worked.
  2. Verified user.id matches the _id in MongoDB.
  3. The eq operator seems to work as expected.
  4. This may be an issue in the internal query parser that maps operators like $ne → ne.
Originally created by @pneumaticos on GitHub (Oct 7, 2025). **I have resulted to using `eq` filter then filtering the data received to exclude the logged in user. Using `id` instead of `_id` seemed not to work** ### Is this suited for github? - [x] Yes, this is suited for github ### To Reproduce On the backend, try accessing the users: ``` import { auth } from "@/lib/auth/auth"; import { headers } from "next/headers"; export default async function StaffManagementPage() { const session = await auth.api.getSession({ headers: await headers(), }); const user = session.user; const users = await auth.api.listUsers({ headers: await headers(), query: { limit: 10, filterField: "_id", filterOperator: "ne", filterValue: user.id, }, }); console.log("Current user:", user.id); console.log("Returned users:", users); } ``` ### Current vs. Expected behavior The currently logged in user should be excluded but he isn't. ### What version of Better Auth are you using? 1.3.27 ### System info ```bash { "system": { "platform": "win32", "arch": "x64", "version": "Windows 10 Pro", "release": "10.0.19045", "cpuCount": 8, "cpuModel": "Intel(R) Core(TM) i5-8365U CPU @ 1.60GHz", "totalMemory": "15.82 GB", "freeMemory": "2.90 GB" }, "node": { "version": "v22.19.0", "env": "development" }, "packageManager": { "name": "npm", "version": "11.5.2" }, "frameworks": [ { "name": "next", "version": "15.5.2" }, { "name": "react", "version": "19.1.0" } ], "databases": [ { "name": "mongodb", "version": "6.18.0" } ], "betterAuth": { "version": "^1.3.27", "config": null } } ``` ### Which area(s) are affected? (Select all that apply) Backend ### Auth config (if applicable) ```typescript import { betterAuth } from "better-auth"; import { MongoClient } from "mongodb"; import { mongodbAdapter } from "better-auth/adapters/mongodb"; import { authClient } from "./auth-client"; import { logSuccess } from "../logger"; import { nextCookies } from "better-auth/next-js"; import { admin as adminPlugin, multiSession } from "better-auth/plugins"; import { ac, admin, staff } from "./permissions"; const client = new MongoClient(process.env.MONGODB_URI_LINK!); const db = client.db(); client.on("open", () => { logSuccess("✅ MongoDB connection opened"); }); export const auth = betterAuth({ emailAndPassword: { enabled: true, async sendResetPassword(data) { // Send an email to the user with a link to reset their password console.log(data); }, }, database: mongodbAdapter(db, { client, }), user: { additionalFields: { role: { type: ["staff", "admin"], required: true, defaultValue: "admin", }, onboarding: { type: "boolean", required: true, defaultValue: false, }, restaurantId: { type: "string", required: false, }, }, }, trustedOrigins: ["http://192.168.100.21:3000"], // session: { // cookieCache: { // enabled: true, // maxAge: 20 * 60, // }, // }, plugins: [ adminPlugin({ ac, roles: { staff, admin, }, defaultRole: "admin", }), multiSession({ maximumSessions: 1, }), nextCookies(), ], }); export type Session = typeof authClient.$Infer.Session; ``` ### Additional context 1. Tried both `filterField: "id"` and `filterField: "_id"` - only `_id` worked. 2. Verified user.id matches the _id in MongoDB. 3. The eq operator seems to work as expected. 4. This may be an issue in the internal query parser that maps operators like $ne → ne.
GiteaMirror added the bug label 2026-03-13 09:26:03 -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#2086