最新消息:20210917 已从crifan.com换到crifan.org

【已解决】MongoDB的MongoDB Compass中用正则进行模糊匹配字段

MongoDB crifan 5464浏览 0评论

现有MongoDB数据库:

比如:AD450L

希望在Compass中用正则去模拟匹配出:

“AD=Adult Directed:

家长指导书籍。这类读物一般都是带有文字的绘本,适合家长陪同学龄前儿童一起阅读。

NC=Non-Conforming:

非常规书籍。这类读物的语言难度一般超过了目标读者的阅读能力。适合阅读能力高于平均水平的读者阅读。

HL=High-Low:

趣味性高但难度低的书籍。适合较高年级阅读能力较低的学生。

IG=Illustrated Guide:

图释。一般是百科全书。

GN=Graphic Novel:

连环画或漫画。

BR=Beginning Reading:

初级读物。

NP=Non-Prose:

非散文性文章。如诗歌、歌词或者菜谱。此类文章无法评定兰斯等级。”

的兰斯的数据的例子

参考自己之前的:

【已解决】Mongo中让搜索支持不区分大小写

的语法:

file_list = [[str(i._id), i.filename] for i in grid_fs_collection.find({‘metadata.fileInfo.isAudio’: True, "filename": {"$regex": search, "$options": "i"}})]

可支持 不区分大小写去搜索文件名了

以及具体语法参考:

$regex

结果试了半天:

{"grading": {"$regex": "[A-Z]{2}\d+[A-Z]", "$options": "i"}}

{"grading.lexile": {"$regex": "[A-Z]{2}\d+[A-Z]", "$options": "i"}}

{"grading.lexile": {"$regex": "[A-Z]{2}\d+[A-Z]"}}

{"grading.lexile": {$regex: "[A-Z]{2}\d+[A-Z]"}}

{grading.lexile: {$regex: "[A-Z]{2}\d+[A-Z]"}}

{"grading.lexile": {$regex: "\w+"}}

竟然都搜不到:

mongodb 正则 搜索

MongoDB 正则表达式 | 菜鸟教程

Mongodb正则表达式$regex操作符 – 明洋的专栏 – CSDN博客

正则表达式 – Mongodb 教程 – 极客学院Wiki

mongodb正则查询使用须知 – yaksea – 博客园

3分钟掌握MongoDB中的regex几种用法-贺磊的技术博客-51CTO博客

MongoDB高级查询 / $regex正则表达式匹配 – 汇智网

感觉或许是此处是二级字段?

mongodb second level field

selecting by "second level" key on mongodb – Stack Overflow

MongoDB how to find record with "second level" value – Stack Overflow

就是用点分隔开即可

grading.lexile

Query on Embedded/Nested Documents — MongoDB Manual

试了:

{"grading.lexile": {$regex: "^[A-Z]{2}\d+[A-Z]$"}}

{"grading.lexile": {"$regex": "^[A-Z]{2}\d+[A-Z]$"}}

还是不行。

试了:

{"grading.lexile": "AD450L"}

是可以的。

mongodb compass regex search

Querying in MongoDB Compass – Stack Overflow

{"grading.lexile": "AD*"}

{"grading.lexile": "/AD*/"}

{"grading.lexile": "/AD.*/"}

Mongodb Compass regex search

MongoDB Compass | Andrew Morgan on Databases

Mongodb Compass filter regex

"Mongodb Compass" filter regex

mongodb – regex as $filter in projection – Stack Overflow

Compass Queries – Google Groups

{"grading.lexile": {"$regex": "^AD\d+.*$"}}

{"grading.lexile": {$regex: "^AD\d+.*$"}}

最后暂时用:

{"grading.lexile": {$regex: "^AD.*$"}}

或:

{"grading.lexile": {$regex: "AD.*"}}

可以搜到:

 

比如再去换个别的:

{"grading.lexile": {$regex: "HL.*"}}

【总结】

MongoDB Compass中,想要用正则搜索字段:

grading

    lexile: "AD450L"

写法是:

{"grading.lexile": {$regex: "AD.*"}}

或:regex加上行首和行尾判断:

{"grading.lexile": {$regex: "^AD.*$"}}

或 regex用引号引起来

{"grading.lexile": {"$regex": "AD.*"}}

才可以搜到数据:

注意:

如果写成:

{"grading.lexile": {$regex: "^AD\d+.*$"}}

是不行的-》貌似此处不支持\d表示数字。。。

后记:

比如:

想要搜:

用:

{"title": {"$regex": "Henry.*"}}

{"title": {"$regex": "Henry and Mudge.*"}}

{"title": {"$regex": "Henry and Mudge Get the .*"}}

{"title": {"$regex": "Henry and Mudge Get the Cold Shivers"}}

即可:

转载请注明:在路上 » 【已解决】MongoDB的MongoDB Compass中用正则进行模糊匹配字段

发表我的评论
取消评论

表情

Hi,您需要填写昵称和邮箱!

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址
92 queries in 0.194 seconds, using 23.34MB memory