【问题标题】:How to make auto suggestion in mean stack如何在平均堆栈中提出自动建议
【发布时间】:2020-07-27 17:11:39
【问题描述】:

我有这个代码:

async function getUserByArtistName(artistName) {
  let userDB = await User.find(
      {$text: { $search: artistName}},
      (err, res) => {
        if(err){
          console.log("ERROR : ")
          console.log(err)
        } else {
          console.log(res)
        }
      });
  return userDB;
}

我需要找到 User.artistName 包含 * ArtistName * 的用户。有可能吗?

非常感谢!

【问题讨论】:

  • $text 需要文本索引,因此它将搜索您在创建索引时指定的字段。
  • 我已经在我的架构上创建了索引

标签: mongodb mongoose mean-stack mongoose-schema


【解决方案1】:

答案:

async function getUserByArtistName(artistName) {
  let userDB = await User.find(
      { artistName: { $regex: artistName, $options: "i" } },
      (err, res) => {
        if(err){
          console.log("ERROR : ")
          console.log(err)
        } else {
          console.log(res)
        }
      });
  return userDB;
}

【讨论】:

    猜你喜欢
    • 2018-11-29
    • 1970-01-01
    • 2014-05-17
    • 2016-09-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-09-01
    相关资源
    最近更新 更多