【问题标题】:How to filter with text matching?如何使用文本匹配进行过滤?
【发布时间】:2019-09-02 16:14:10
【问题描述】:

我想在 mongodb 和节点中查询匹配文本。

我尝试将发送的参数与开头和结尾的斜线连接,但它不起作用。

const {filterTerm} = req.body;
    const slash1 = "/";
    const slash2 = "/";

    const term = slash1.concat(filterTerm);
    const finalTerm = term.concat(slash2);

    console.log(finalTerm);

    Bookings.find({$or: [{email: finalTerm}, {phone: finalTerm}]})
        .exec((err, docs) => {
            res.json(docs);
        });`

【问题讨论】:

  • 你在找$regex
  • 我刚接触 mongo 两天大。我想在 sql 中完成例如:%searchTerm%

标签: node.js mongoose


【解决方案1】:

使用正则表达式

const finalTerm = term.concat(slash2);
Bookings
  .find({$or: [{email: new RegExp(finalTerm, 'gi') }, {phone: new RegExp(finalTerm, 'gi') }]})
  .exec((err, docs) => {
      res.json(docs);
  });

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2016-06-01
    • 2011-05-04
    • 1970-01-01
    • 2021-05-31
    • 2023-03-13
    • 2019-04-11
    相关资源
    最近更新 更多