节点部分搜索

Node partial search

我在网上找到了教程,其中有一种简单的方法可以进行部分搜索。我想做的是当有人只写 geor 时,它会找到一个名为 george

的用户
db.stores.find({storeName : {$regex : /Geor/}}).pretty()

但我不知道如何在我的搜索中实现它。我尝试了一些东西,但一切都以错误结束。

这是我的控制器:

/**
 * GET /admin/users/:page
 * Find users
 */
exports.findUsers = (req, res, next) => {
  const perPage = 13
  const page = Number(req.params.page) || 1

  var query = {};

  const possibleQueryProps = [{
      "key": "id",
      "queryKey": "_id"
    },
    {
      "key": "email",
      "queryKey": "email"
    },
    {
      "key": "firstname",
      "queryKey": "profile.firstname"
    },
    {
      "key": "lastname",
      "queryKey": "profile.lastname"
    },
    {
      "key": "location",
      "queryKey": "profile.location"
    },
    {
      "key": "status",
      "queryKey": "profile.status"
    }
  ];

  possibleQueryProps.forEach(prop => {
    if (req.query[prop.key]) {
      query[prop.queryKey] = req.query[prop.key];
    }
  });
  console.log(query)
  User
    .find(query)
    .skip((perPage * page) - perPage)
    .limit(perPage)
    .exec(function (err, users) {
      User.countDocuments(query).exec(function (err, count) {
        if (err) return next(err)
        res.render('admin/users', {
          layout: "admin",
          users: users,
          query: req.query,
          active: {
            users: true
          },
          current: page,
          pages: Math.ceil(count / perPage),
          helpers,
          pagesArr: Array.from(Array(((Math.ceil(count / perPage)))).keys()).map(i => 1 + i),
          pagination: core.pagination(page, (Math.ceil(count / perPage))),
        })
      })
    })
};

如有任何建议,我将不胜感激。

我认为它会起作用:

possibleQueryProps.forEach(prop => {
  if (req.query[prop.key]) {
    query[prop.queryKey] = {$regex : new RegExp('.*' + req.query[prop.key] + '.*')};
  }
});