在mongodb中通过名字+姓氏获取用户

问题描述 投票:0回答:1

我的应用程序中的搜索无法正常工作。例如:如果姓名是“John Smith”并且我仅按 John (first_name) 搜索,我会按姓名 John 获取所有文档,如果我仅输入“Smith”,那么它也可以正常工作,但是当我写全名 John(first_name) 时史密斯(姓氏)没有返回任何文件。当我在搜索框中输入全名时,我想返回文档。

代码如下: getUsersWithSearchParamAndLimit => 返回文档

注意:下面函数中的搜索词是来自我前端搜索字段的内容。如果我输入“John Smith”,那么它会在下面的函数中进行查询

exports.getUsersWithSearchParamAndLimit = async (req, res) => {
  const { searchterm, role } = req.body;
  const page = req.params.page;
  const limit = req.params.limit;
  const skip = (page - 1) * limit;

  const users = await User.find({
    $and: [
      {
        $or: [
          { first_name: { $regex: searchterm, $options: "i" } },
          { last_name: { $regex: searchterm, $options: "i" } },
          { billing_phone: { $regex: searchterm, $options: "i" } },
          { user_email: { $regex: searchterm, $options: "i" } },
        ],
      },
      { role: role.length > 0 ? role : { $in: ["customer", "admin"] } },
    ],
  })
    .select(
      "first_name last_name user_email user_verified billing_phone userVerified createdAt disable"
    )
    .limit(limit)
    .skip(skip)
    .sort({ createdAt: "desc" });

  const count = await User.countDocuments({
    $and: [
      {
        $or: [
          { first_name: { $regex: searchterm, $options: "i" } },
          { last_name: { $regex: searchterm, $options: "i" } },
          { billing_phone: { $regex: searchterm, $options: "i" } },
          { user_email: { $regex: searchterm, $options: "i" } },
        ],
      },
      { role: role.length > 0 ? role : { $in: ["customer", "admin"] } },
    ],
  }).exec();

  return res.json({
    status: "SUCCESS",
    users: users,
    count: count,
  });
};

我的 mongodb 用户架构如下:

{
  "_id": {
    "$oid": "___________________"
  },
  "id": "11547",
  "first_name": "XXX",
  "last_name": "XX",
  "user_email": "asbc",
  "user_pass": "*****",
  "role": "customer",
  "billing_phone": "1230123",
  "birth_date": "11/07/20",
  "userVerified": true,
  "createdAt": {
    "$date": "2022-05-17T13:50:30.358Z"
  },
  "updatedAt": {
    "$date": "2023-09-11T18:14:39.221Z"
  },
  "__v": 0,
  "disable": false,
  "billingAddress": {
    "city": "Drayton Valley",
    "postalCode": "XXLSSL",
    "province": "Alberta",
    "streetAddress": "26"
  }
}

javascript mongodb mongoose mongodb-query aggregate
1个回答
0
投票

您可以将

first_name
加入一个空格,然后将
last_name
$concat
一起加入,然后使用
$regexMatch
查找与
regex
组合的全名组合。它看起来像这样:

const users = await User.find({
  $and: [
    {
      $or: [
        {
          $expr: {
            $regexMatch: {
              input: {
                $concat: [
                  "$first_name",
                  " ",
                  "$last_name"
                ]
              },
              regex: searchterm,
              options: "i"
            }
          }
        },
        {
          billing_phone: {
            $regex: searchterm,
            $options: "i"
          }
        },
        {
          user_email: {
            $regex: searchterm,
            $options: "i"
          }
        }
      ]
    },
    {
      role: {
        $in: [
          "customer",
          "admin"
        ]
      }
    }
  ]
})

然后您可以将

select
limit
skip
sort
阶段链接到它。

请参阅此处了解工作示例。

注意

billing_phone
需要是一个字符串。你不能对数字进行正则表达式。

© www.soinside.com 2019 - 2024. All rights reserved.