【问题标题】:GridFS with pymongo cannot retrieve files带有pymongo的GridFS无法检索文件
【发布时间】:2013-11-30 17:57:28
【问题描述】:

我尝试测试 GridFS,但无法通过: 我已将文件放入本地 gridfs。

$ mongofiles list
connected to: 127.0.0.1
IMGP2224.JPG     1125745

在我的代码中:

from pymongo import MongoClient
import gridfs
...
def index():
     db = MongoClient('localhost', 27017).gridfs
     files = gridfs.GridFS(db)
     image = files.list()
     import pdb;pdb.set_trace()
     **pdb here gives empty []**
     return render_template("index.html", images=image)

我做错了什么?

【问题讨论】:

    标签: mongodb pymongo gridfs


    【解决方案1】:

    如果您在使用mongofiles 时未提供数据库名称,它将使用默认数据库(称为test)。如果不指定集合名称,它将使用fs.filesfs.chunks

    因此,在您的 Python 代码中,您需要使用 test 作为数据库名称,而不是 gridfs,然后它将找到您的默认 fs.filesfs.chunks 集合。

    【讨论】:

    • 好吧,我得到的列表没问题。在测试数据库的 mongo shell 中: db.fs.files.find() 给出 {"_id": ObjectId("5287b92d53aad8ef5062a08c", "filename" : "IMGP2224.JPG".......} 但在 python 代码中: files.exists(filename="IMGP2224.JPG") True files.get("5287b92d53aad8ef5062a08c") NoFile:gridfs 集合中没有文件
    • 不要做:files.get("5287b92d53aad8ef5062a08c")。做:files.get(ObjectId("5287b92d53aad8ef5062a08c"))。 ObjectId 和字符串彼此不同。
    猜你喜欢
    • 2014-09-23
    • 1970-01-01
    • 2020-10-23
    • 2013-01-02
    • 1970-01-01
    • 2018-05-25
    • 2016-01-01
    • 2014-03-29
    • 2017-12-31
    相关资源
    最近更新 更多