From 13b2a764efd96efd6369d10a5870f563f05f5b25 Mon Sep 17 00:00:00 2001 From: yuzhiran Date: Mon, 22 Jun 2026 12:14:31 +0800 Subject: [PATCH] fix(admin): partial _id fuzzy search via $expr + $toString + $regexMatch --- backend/src/modules/admin/admin.controller.ts | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/backend/src/modules/admin/admin.controller.ts b/backend/src/modules/admin/admin.controller.ts index b231348..52c6bdd 100644 --- a/backend/src/modules/admin/admin.controller.ts +++ b/backend/src/modules/admin/admin.controller.ts @@ -88,10 +88,10 @@ export class AdminController { { email: { $regex: escaped, $options: 'i' } }, { wxOpenid: { $regex: escaped, $options: 'i' } }, ] - // 支持按 MongoDB _id 搜索(24位 hex) - if (/^[0-9a-f]{24}$/i.test(keyword)) { - filter.$or.push({ _id: keyword }) - } + // 支持按 _id 模糊搜索(ObjectId → string → regex) + filter.$or.push({ + $expr: { $regexMatch: { input: { $toString: '$_id' }, regex: escaped, options: 'i' } }, + }) } const skip = (Math.max(1, +page) - 1) * +limit const [users, total] = await Promise.all([