查询运行时删除Mongo聚集的特殊字符

发布于 2025-01-21 05:29:09 字数 442 浏览 0 评论 0原文

字符时从mongo中获取所有数据

我想在需要替换

[{
number: "12/34",
type: "string"
},
{
number: "56-78",
type: "string"
},
{
number: "910*2",
type: "string"
}]

特殊 代码>

相关输出是:

[{
number: "12/34",
type: "string"
},
{
number: "56-78",
type: "string"
}]

我尝试的是没有特殊字符的情况下添加临时字段,但是我不能删除所有字段,因为AddField无法处理正则是Regex。我尝试用减少来做到这一点,但这对我不起作用。 有人可以帮助我吗?

I would like to get all the datas from mongo while i need to replace special characters, but not rewrite the data while the query run:

data in db:

[{
number: "12/34",
type: "string"
},
{
number: "56-78",
type: "string"
},
{
number: "910*2",
type: "string"
}]

the number what I would like to query is: 1234, 5678

the related output is:

[{
number: "12/34",
type: "string"
},
{
number: "56-78",
type: "string"
}]

what I try is to add field temporary without special characters, but I cannot remove all of them because addfield doesn't handle regex. i try to do this with reduce but it's not work for me.
Anybody can help me?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

许久 2025-01-28 05:29:09

使用MongoDB v4.2+,您可以使用$ regexfindall找到所有数字。使用$降低$ concat以删除/消毒的特殊字符重建字符串。在消毒字符串上执行搜索。

db.collection.aggregate([
  {
    "$addFields": {
      "sanitized": {
        "$regexFindAll": {
          "input": "$number",
          "regex": "\\d"
        }
      }
    }
  },
  {
    "$addFields": {
      "sanitized": {
        "$reduce": {
          "input": "$sanitized.match",
          "initialValue": "",
          "in": {
            "$concat": [
              "$value",
              "$this"
            ]
          }
        }
      }
    }
  },
  {
    "$match": {
      $expr: {
        "$in": [
          "$sanitized",
          [
            "1234",
            "5678"
          ]
        ]
      }
    }
  },
  {
    "$project": {
      sanitized: false
    }
  }
])

这是 mongo Playground 供您参考。

With MongoDB v4.2+, you can use $regexFindAll to locate all digits. Use $reduce and $concat to reconstruct a string with special characters removed/sanitized. Perform the search on the sanitized string.

db.collection.aggregate([
  {
    "$addFields": {
      "sanitized": {
        "$regexFindAll": {
          "input": "$number",
          "regex": "\\d"
        }
      }
    }
  },
  {
    "$addFields": {
      "sanitized": {
        "$reduce": {
          "input": "$sanitized.match",
          "initialValue": "",
          "in": {
            "$concat": [
              "$value",
              "$this"
            ]
          }
        }
      }
    }
  },
  {
    "$match": {
      $expr: {
        "$in": [
          "$sanitized",
          [
            "1234",
            "5678"
          ]
        ]
      }
    }
  },
  {
    "$project": {
      sanitized: false
    }
  }
])

Here is the Mongo playground for your reference.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文