查询运行时删除Mongo聚集的特殊字符
字符时从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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
使用MongoDB v4.2+,您可以使用
$ regexfindall
找到所有数字。使用$降低
和$ concat
以删除/消毒的特殊字符重建字符串。在消毒字符串上执行搜索。这是 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.Here is the Mongo playground for your reference.