最新消息:20210816 当前crifan.com域名已被污染,为防止失联,请关注(页面右下角的)公众号

【规避解决】Flask-PyMongo中如何查询gridfs中的文件

Flask crifan 5117浏览 0评论

折腾:

【已解决】Flask中连接远程MongoDB数据库的gridfs并返回查询到的文件数据

期间,用代码去find:

mongoDb = mongo.db

log.info("mongoDb=%s", mongoDb)

gridfsDb = mongoDb.gridfs

log.info("gridfsDb=%s", gridfsDb)

sleepFile = gridfsDb.fs.find({"name": "sleep"})

log.info("sleepFile=%s", sleepFile)

但是调试看到的是cursor,并不是以为的一个list:

所以需要搞清楚,如何才能获得返回的文件的dict的list

看到:

Flask扩展系列(五)–MongoDB – 思诚之道

提到了:

““find_one()”方法返回的就是一个字典,所以我们可以直接对其作操作。”find()”方法返回的其实是一个”pymongo.cursor.Cursor”对象,不过Cursor类实现了”__iter__()”和”next()”方法,因此可以用”for … in …”循环来遍历它。”

再去看看:

cursor – Tools for iterating over MongoDB query results — PyMongo 3.6.1 documentation

先试试count,不会这里find到的是0吧?

果然是0:

我说find不到呢。

估计是自己参数传递错了

看数据库,则是:

去搜filename才可以

但是通过工具发现:

要全名匹配才可以:

否则部分文件名去搜,返回是空的:

所以要再去搞清楚:

Mongo中如何搜索部分文件名

gridfs search part filename

gridfs search file by filename

mongodb – Ruby GridFS – Search on filename – Stack Overflow

mongo search file by filename

mongo gridfs find

python mongo gridfs find

gridfs – Tools for working with GridFS — PyMongo 2.8 documentation

好像没有根据文件名的模糊搜索

python mongo gridfs wildcard find

mongodb – Performing regex Queries with pymongo – Stack Overflow

去试试regex,结果还是找不到文件:

python – REGEX Searching in pymongo – Stack Overflow

$regex — MongoDB Manual 3.6

好像是有问题的,需要用$regex才可以

还是不行:

才发现,是数据库的collection搞错了:

结果:

竟然还是0:

结果:

gridfsDb = mongoDb.gridfs

log.info("gridfsDb=%s", gridfsDb)

# fsCollection = GridFS(gridfsDb)

# log.info("fsCollection=%s", fsCollection)

findRegex = re.compile("Go", re.IGNORECASE)

log.info("findRegex=%s", findRegex)

# findFileCursor = gridfsDb.fs.find({"filename": findRegex})

# findFileCursor = gridfsDb.fs.find({"filename": {"$regex": findRegex, "$options": "i"}})

# findFileCursor = gridfsDb.fs.find({"filename": {"$regex": "Go", "$options": "i"}})

# findFileCursor = gridfsDb.fs.find({"filename": {"$regex": "Go"}})

# findFileCursor = gridfsDb.fs.files.find({"filename": {"$regex": "Go"}})

# findFileCursor = gridfsDb.fs.files.find({"filename": "Go away, Mr Wolf.mp3"})

# findFileCursor = gridfsDb.fs.files.find({"filename": "Go away, Mr Wolf.mp3"})

findFileCursor = gridfsDb.fs.files.find()

log.info("findFileCursor=%s", findFileCursor)

findFileCount = findFileCursor.count()

log.info("findFileCount=%s", findFileCount)

竟然都还是0:

看来肯定是哪里搞错了。

python mongo gridfs can not find filename

结果试了多次,(在别的项目中可以工作的mongod的gridfs的)代码:

# fsCollection = gridfsDb.fs

# fsCollection = gridfsDb["fs"]

fsCollection = GridFS(gridfsDb)

logging.info("fsCollection=%s", fsCollection)

# findFileCursor = fsCollection.files.find()

# findFileCursor = fsCollection.files.find_one()

# findFileCursor = fsCollection.files.find_one()

findFileCursor = fsCollection.find_one()

# findFileCursor = fsCollection.find()

log.info("findFileCursor=%s", findFileCursor)

findFileCount = findFileCursor.count()

log.info("findFileCount=%s", findFileCount)

还出错:

  File "/Users/crifan/dev/dev_root/company/naturling/projects/robotDemo/testRestApi.py", line 89, in get

    fsCollection = GridFS(gridfsDb)

  File "/Users/crifan/.virtualenvs/robotDemo-HXjMJQEQ/lib/python3.6/site-packages/gridfs/__init__.py", line 59, in __init__

    raise TypeError("database must be an instance of Database")

TypeError: database must be an instance of Database

pymongo gridfs

Flask-PyMongo gridfs find

Just a little Python: GridFS: The MongoDB Filesystem

Flask-PyMongo — Flask-PyMongo 0.5.1 documentation

Flask-PyMongo — Flask-PyMongo 0.4.0 documentation

然后发现:

此处的gridfs,不论是fs还是fs.files的fine_one,都是None

感觉是:flask-pymongo 有问题?

和我直接用pymongo有啥区别?

flask-pymongo vs pymongo

python – Difference between PyMongo and Flask-PyMongo libraries – Stack Overflow

好像没啥多大好处

Python Tutorial: MongoDB RESTful API with Flask – 2018

python – PyMongo on the Flask – Stack Overflow

mongodb – What is the relationship between flask, mongokit, pymongo, flask-pymongo? – Stack Overflow

database – What is the difference between an ORM and an ODM? – Stack Overflow

MongoDB Flaskers, what’s your opinion on Ming vs MongoEngine vs directly using PyMongo?:flask

How Do You Use MongoDB With Python?

大家用什么 MongoDB 的 ORM 来配合 Flask – V2EX

Tools — PyMongo 2.7 documentation

算了,去单独使用pymono试试

# pure use pymongo

gridfsDb2 = mongoClient.gridfs

logging.info("gridfsDb2=%s", gridfsDb2)

fsCollection2 = GridFS(gridfsDb2)

logging.info("fsCollection2=%s", fsCollection2)

findFileCursor = fsCollection2.find()

log.info("findFileCursor=%s", findFileCursor)

findFileCount = findFileCursor.count()

log.info("findFileCount=%s", findFileCount)

然后就是可以搜索到文件的:

然后相关代码:

# mongoClient = MongoClient(mongodbUri)

mongoClient = MongoClient(

    host=“x.x.x.x",

    port=xxxx,

    username="gridfs",

    password=“yyy",

    authSource="gridfs"

)

log.info("mongoClient=%s", mongoClient)

# pure use pymongo

gridfsDb2 = mongoClient.gridfs

logging.info("gridfsDb2=%s", gridfsDb2)

fsCollection2 = GridFS(gridfsDb2)

logging.info("fsCollection2=%s", fsCollection2)

# findFileCursor = fsCollection2.find()

findFileCursor = fsCollection2.find({"filename": {"$regex": "sleep", "$options": "i"}})

log.info("findFileCursor=%s", findFileCursor)

findFileCount = findFileCursor.count()

log.info("findFileCount=%s", findFileCount)

for curIdx, eachFile in enumerate(findFileCursor):

    curNum = curIdx + 1

    log.info("[%d] _id=%s, filename=%s", curNum, eachFile._id, eachFile.filename)

再去调试,就可以找到文件了:

那看起来是:

要么是我自己不会用flask-pymongo

要么是flask-pymongo有问题?

flask-pymongo not work

flask-pymongo not work find none

python – PyMongo returns an empty result set when a MongoDB client returns correct results – Stack Overflow

flask-pymongo not work

此处经过调试代码:

app.config.update(

    MONGO_HOST=‘xxx’,

    MONGO_PORT=xxx,

    MONGO_USERNAME=’gridfs’,

    MONGO_PASSWORD=‘xxx’,

    MONGO_DBNAME=’gridfs’

)

flaskPymongo = PyMongo(app)

log.info("flaskPymongo=%s", flaskPymongo)

# purePymongo = MongoClient(mongodbUri)

purePymongo = MongoClient(

    host=“xxx",

    port=xx,

    username="gridfs",

    password=“xxx",

    authSource="gridfs"

)

log.info("purePymongo=%s", purePymongo)

# Flask-PyMongo

mongoDb = flaskPymongo.db #Database(MongoClient(host=[‘x.x.x.x:12345‘], document_class=dict, tz_aware=True, connect=True, replicaset=None), ‘gridfs’)

log.info("mongoDb=%s", mongoDb)

gridfsDb = mongoDb.gridfs #Collection(Database(MongoClient(host=[‘x.x.x.x:12345‘], document_class=dict, tz_aware=True, connect=True, replicaset=None), ‘gridfs’), ‘gridfs’)

log.info("gridfsDb=%s", gridfsDb)

fsCollection = gridfsDb.fs #Collection(Database(MongoClient(host=[‘x.x.x.x:12345‘], document_class=dict, tz_aware=True, connect=True, replicaset=None), ‘gridfs’), ‘gridfs.fs’)

log.info("fsCollection=%s", fsCollection)

filesCollection = fsCollection.files # Collection(Database(MongoClient(host=[‘x.x.x.x:12345‘], document_class=dict, tz_aware=True, connect=True, replicaset=None), ‘gridfs’), ‘gridfs.fs.files’)

log.info("filesCollection=%s", filesCollection)

# # Pure PyMongo

# gridfsDb = purePymongo.gridfs #Database(MongoClient(host=[‘x.x.x.x:12345‘], document_class=dict, tz_aware=False, connect=True, authsource=’gridfs’), ‘gridfs’)

# logging.info("gridfsDb=%s", gridfsDb)

# fsCollection = GridFS(gridfsDb) #<gridfs.GridFS object at 0x1107b2390>

# logging.info("fsCollection=%s", fsCollection)

fsFindOne = fsCollection.find_one() #<gridfs.grid_file.GridOut object at 0x110a87fd0>

log.info("fsFindOne=%s", fsFindOne)

filesFindOne = filesCollection.find_one()

log.info("filesFindOne=%s", filesFindOne)

发现:

Flask-PyMongo得到的fs或fs.file的Collection去fine_one,都是None

而纯的PyMongo得到的fs的Collection去fine_one,就可以find到文件

看起来像是:flask-pymongo中,对于gridfs本身支持有问题?

flask-pymongo gridfs

share/access db connection · Issue #101 · dcrosta/flask-pymongo

A simple GridFS server built with Flask

flask-pymongo 不支持 gridfs

去试了试grifs的fine_one也是None:

gridFineOne = gridfsDb.find_one() #

log.info("gridFineOne=%s", gridFineOne)

算了,找不到相关的内容

就不继续折腾了。

【总结】

此处Flask-PyMongo,对于gridfs来说,不论是gridfs,还是gridfs.fs,还是grifs.fs.files,都无法通过find或fine_one去找到文件。搞不清楚为何。

最后只能用纯的PyMongo才可以。

具体代码:

# from flask_pymongo import PyMongo

from gridfs import GridFS

from pymongo import MongoClient

# Flask-PyMongo NOT work: gridfs can NOT find files

# app.config.update(

#     MONGO_HOST=’x.x.x.x‘,

#     MONGO_PORT=12345,

#     MONGO_USERNAME=’gridfs’,

#     MONGO_PASSWORD=’P@wd’,

#     MONGO_DBNAME=’gridfs’

# )

# flaskPymongo = PyMongo(app)

# log.info("flaskPymongo=%s", flaskPymongo)

# purePymongo = MongoClient(mongodbUri)

purePymongo = MongoClient(

    host="x.x.x.x",

    port=12345,

    username="gridfs",

    password=“xxx",

    authSource="gridfs"

)

log.info("purePymongo=%s", purePymongo)

# # Flask-PyMongo

# mongoDb = flaskPymongo.db #Database(MongoClient(host=[‘x.x.x.x:12345‘], document_class=dict, tz_aware=True, connect=True, replicaset=None), ‘gridfs’)

# log.info("mongoDb=%s", mongoDb)

# gridfsDb = mongoDb.gridfs #Collection(Database(MongoClient(host=[‘x.x.x.x:12345‘], document_class=dict, tz_aware=True, connect=True, replicaset=None), ‘gridfs’), ‘gridfs’)

# log.info("gridfsDb=%s", gridfsDb)

# fsCollection = gridfsDb.fs #Collection(Database(MongoClient(host=[‘x.x.x.x:12345‘], document_class=dict, tz_aware=True, connect=True, replicaset=None), ‘gridfs’), ‘gridfs.fs’)

# log.info("fsCollection=%s", fsCollection)

# filesCollection = fsCollection.files # Collection(Database(MongoClient(host=[‘x.x.x.x:12345‘], document_class=dict, tz_aware=True, connect=True, replicaset=None), ‘gridfs’), ‘gridfs.fs.files’)

# log.info("filesCollection=%s", filesCollection)

# Pure PyMongo

gridfsDb = purePymongo.gridfs #Database(MongoClient(host=[‘x.x.x.x:12345‘], document_class=dict, tz_aware=False, connect=True, authsource=’gridfs’), ‘gridfs’)

logging.info("gridfsDb=%s", gridfsDb)

fsCollection = GridFS(gridfsDb) #<gridfs.GridFS object at 0x1107b2390>

logging.info("fsCollection=%s", fsCollection)

fsFindOne = fsCollection.find_one() #<gridfs.grid_file.GridOut object at 0x110a87fd0>

log.info("fsFindOne=%s", fsFindOne)

# gridFineOne = gridfsDb.find_one() #

# log.info("gridFineOne=%s", gridFineOne)

#

# filesFindOne = filesCollection.find_one()

# log.info("filesFindOne=%s", filesFindOne)

# findRegex = re.compile("Go", re.IGNORECASE)

findRegex = re.compile("sleep", re.IGNORECASE)

log.info("findRegex=%s", findRegex)

# findFileCursor = gridfsDb.fs.find({"filename": findRegex})

# findFileCursor = gridfsDb.fs.find({"filename": {"$regex": findRegex, "$options": "i"}})

# findFileCursor = fsCollection.find({"filename": {"$regex": "sleep"}})

findFileCursor = fsCollection.find({"filename": {"$regex": "sleep", "$options": "i"}})

log.info("findFileCursor=%s", findFileCursor)

findFileCount = findFileCursor.count()

log.info("findFileCount=%s", findFileCount)

for curIdx, eachFile in enumerate(findFileCursor):

    curNum = curIdx + 1

    log.info("[%d] _id=%s, filename=%s", curNum, eachFile._id, eachFile.filename)

效果:

注:

(1)此处另外两种写法:

findRegex = re.compile("sleep", re.IGNORECASE)

log.info("findRegex=%s", findRegex)

# findFileCursor = gridfsDb.fs.find({"filename": findRegex})

# findFileCursor = gridfsDb.fs.find({"filename": {"$regex": findRegex, "$options": "i"}})

都是不行的。

(2)此处单独搜索sleep,如果区分大小写,-》即不加i=ignore up lower case参数的话:

由于gridfs中两个文件:

filename=1004 Horrid Henry’s Sleepover.mp3

filename=2.Good Night, Sleep Tight.m4a

都没有包含sleep

所以就搜不到文件了,搜出来是None了:

转载请注明:在路上 » 【规避解决】Flask-PyMongo中如何查询gridfs中的文件

发表我的评论
取消评论

表情

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

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址
82 queries in 0.174 seconds, using 22.07MB memory