fix(admin): partial _id fuzzy search via $expr + $toString + $regexMatch

This commit is contained in:
yuzhiran
2026-06-22 12:14:31 +08:00
parent 70c4f28eb5
commit 13b2a764ef
@@ -88,10 +88,10 @@ export class AdminController {
{ email: { $regex: escaped, $options: 'i' } }, { email: { $regex: escaped, $options: 'i' } },
{ wxOpenid: { $regex: escaped, $options: 'i' } }, { wxOpenid: { $regex: escaped, $options: 'i' } },
] ]
// 支持按 MongoDB _id 搜索(24位 hex // 支持按 _id 模糊搜索(ObjectId → string → regex
if (/^[0-9a-f]{24}$/i.test(keyword)) { filter.$or.push({
filter.$or.push({ _id: keyword }) $expr: { $regexMatch: { input: { $toString: '$_id' }, regex: escaped, options: 'i' } },
} })
} }
const skip = (Math.max(1, +page) - 1) * +limit const skip = (Math.max(1, +page) - 1) * +limit
const [users, total] = await Promise.all([ const [users, total] = await Promise.all([