【问题标题】:How to make an Exact Phrase text-search in MongoDB using variable (and node.js)如何使用变量(和 node.js)在 MongoDB 中进行精确短语文本搜索
【发布时间】:2021-07-18 07:50:44
【问题描述】:

我正在尝试根据精确短语搜索查找 mongoDB 文档列表。我想首先将确切的短语存储在 variable 中,然后使用 text-search 查询进行搜索。到目前为止,我已经构建了它并且工作正常:

  const wordx = "coffee forest";
  SentenceModel.find({$text:{$search: wordx}}).then(function(records){
    res.send(records);
  });

但是,当我想通过使用转义引号将其用于精确短语搜索时,它不起作用,因为它是一个变量,而不是一个字符串。你知道如何精确短语搜索变量吗? (本网站上的类似问题没有答案)

【问题讨论】:

    标签: node.js mongodb


    【解决方案1】:

    目前还没有答案或评论,但我找到了解决方案,我写在这里是为了大家的利益和未来的参考。

    解决方案包括新的 Javascript 模板文字(如果您使用的是 python,则可以使用 format() 方法)。

    const wordx = "coffee forest";
    const wordy = `\"${wordx}\"`;
    SentenceModel.find({$text:{$search: wordy}}).then(function(records){
      res.send(records);
    });
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2023-03-28
      • 2017-10-02
      • 2020-12-09
      • 1970-01-01
      • 1970-01-01
      • 2019-02-11
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多