Javascript 使用mongodb的搜索查询使用$or返回一个结果

Javascript 使用mongodb的搜索查询使用$or返回一个结果,javascript,regex,express,mongoose,Javascript,Regex,Express,Mongoose,我试图用javascript查询我的MongoDB,但当我应该使用$或时,我似乎得到了一个结果 我要从前端接收一根弦。我的逻辑是按空格分割字符串,然后搜索字符串是否与数据库中的任何技能、位置和名称匹配。例如,如果用户搜索PHP,则应将所有用户的PHP作为一项技能,即使该用户有其他技能。下面是代码 数据 { "_id":"1", "skills": ["PHP"], "name":"You" } { "_id":"2", "skills": ["PHP",

我试图用javascript查询我的MongoDB,但当我应该使用
$或
时,我似乎得到了一个结果

我要从前端接收一根弦。我的逻辑是按空格分割字符串,然后搜索字符串是否与数据库中的任何技能、位置和名称匹配。例如,如果用户搜索PHP,则应将所有用户的
PHP
作为一项技能,即使该用户有其他技能。下面是代码

数据

{
    "_id":"1",
    "skills": ["PHP"],
    "name":"You"
}
{
    "_id":"2",
    "skills": ["PHP", "Javascript"],
    "name":"Me"
}
代码

exports.search = async (req, res, next) => {
  try {
    const escapeChar = escapeString(req.body.query);
    const searchQueries = escapeChar.split(' ');
    let result;
    for (let i in searchQueries) {
      const ret = async () => {
        const pattern = new RegExp(searchQueries[i], 'gi');
        const user = await User.find({
          $or: [{ name: pattern }, { skills: pattern }],
        });
        return user;
      };
      result = await ret();
    }
    if (result.length > 0) {
      return res.json(sendResponse(httpStatus.OK, 'User found', result));
    }
    return res.json(sendResponse(httpStatus.NOT_FOUND, 'User not found'));
  } catch (error) {
      next(error);
  }  
};
假设我搜索
PHP
,我应该得到两个用户。这很有效

但是如果我用“PHP me”搜索,我会得到

{
    "_id":"2",
    "skills ["PHP", "Javascript"],
    "name":"Me"
}

而不是两个用户,因为他们的技能数组中都有
php
。请问问题出在哪里,我该如何解决。

为什么不在查询中使用
$regex

db.getCollection('tests').find({
    $or: [{
        name: {
            $regex: /Me$/
        }
    }, {
        skills: {
            $regex: /PHP$/
        }
    }]
});

上面的查询将提供所需的结果。

为什么不在查询中使用
$regex

db.getCollection('tests').find({
    $or: [{
        name: {
            $regex: /Me$/
        }
    }, {
        skills: {
            $regex: /PHP$/
        }
    }]
});

上面的查询将为您提供所需的结果。

因为您在循环中重新分配了
结果
,所以只有最后一个查询的结果才会出现

您可以使用正则表达式通过一个查询解决问题:

exports.search = async (req, res, next) => {
  try {
    const escapeChar = escapeString(req.body.query);
    const searchQueries = escapeChar.split(' ');

    const pattern = new RegExp(`(${searchQueries.join("|")})`, 'i');
    const users = await User.find({
      $or: [{ name: pattern }, { skills: pattern }],
    });

    // In case the above not works
    const pattern = new RegExp(`(${searchQueries.join("|")})`);
    const users = await User.find({
      $or: [
        {name: {$regex: pattern, $options: "i" }}, 
        {skills: {$regex: pattern, $options: "i" }}
      ],
    });

    if (users.length > 0) {
      return res.json(sendResponse(httpStatus.OK, 'User found', users));
    }
    return res.json(sendResponse(httpStatus.NOT_FOUND, 'User not found'));
  } catch (error) {
    next(error);
  }  
};

因为您在循环中重新分配了
结果
,所以只有最后一个查询的结果才会出现

您可以使用正则表达式通过一个查询解决问题:

exports.search = async (req, res, next) => {
  try {
    const escapeChar = escapeString(req.body.query);
    const searchQueries = escapeChar.split(' ');

    const pattern = new RegExp(`(${searchQueries.join("|")})`, 'i');
    const users = await User.find({
      $or: [{ name: pattern }, { skills: pattern }],
    });

    // In case the above not works
    const pattern = new RegExp(`(${searchQueries.join("|")})`);
    const users = await User.find({
      $or: [
        {name: {$regex: pattern, $options: "i" }}, 
        {skills: {$regex: pattern, $options: "i" }}
      ],
    });

    if (users.length > 0) {
      return res.json(sendResponse(httpStatus.OK, 'User found', users));
    }
    return res.json(sendResponse(httpStatus.NOT_FOUND, 'User not found'));
  } catch (error) {
    next(error);
  }  
};

不起作用。如果我搜索“PHP javascript”,我仍然只会在技能中得到PHP和javascript的数据,而不是两个用户的数据,因为他们都在skillI中有PHP,我注意到如果我搜索“javascript PHP”,两个结果都会出来,但如果我搜索“PHP javascript”只有具备php和javascript技能的用户才知道,为什么不在robo3t或任何其他工具上尝试此查询都不起作用。如果我搜索“PHP javascript”,我仍然只会在技能中得到PHP和javascript的数据,而不是两个用户的数据,因为他们都在skillI中有PHP,我注意到如果我搜索“javascript PHP”,两个结果都会出来,但如果我搜索“PHP javascript”只有具备php和javascript技能的用户才知道,为什么不在robo3t或任何其他工具上尝试此查询