使用pymongo执行正则表达式查询 [英] Performing regex Queries with pymongo

查看:344
本文介绍了使用pymongo执行正则表达式查询的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用pymongo对mongodb服务器执行正则表达式查询.文件结构如下

I am trying to perform a regex query using pymongo against a mongodb server. The document structure is as follows

{
  "files": [
    "File 1",
    "File 2",
    "File 3",
    "File 4"
  ],
  "rootFolder": "/Location/Of/Files"
}

我想获取所有与* File模式匹配的文件.我尝试这样做

I want to get all the files that match the pattern *File. I tried doing this as such

db.collectionName.find({'files':'/^File/'})

但是我什么也没得到,我是否缺少任何东西,因为根据mongodb docs,这应该是可能的.如果我在mongo控制台中执行查询,则可以正常工作,这是否表示api不支持它,或者我只是使用不正确

Yet i get nothing back , am i missing something because according to the mongodb docs this should be possible. If I perform the query in the mongo console it works fine , does this mean the api doesnt support it or am I just using it incorrectly

推荐答案

事实证明,在pymongo中进行正则表达式搜索的方式略有不同,但同样简单.

Turns out regex searches are done a little differently in pymongo but is just as easy.

正则表达式如下:

db.collectionname.find({'files':{'$regex':'^File'}})

这将匹配所有具有files属性的文档,该文档中包含以File开头的项

This will match all documents that have a files property that has a item within that starts with File

这篇关于使用pymongo执行正则表达式查询的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆