mongodb gridfs python出现“no file”错误

2024-09-27 21:32:04 发布

您现在位置:Python中文网/ 问答频道 /正文

我有mongo数据库, 其中我创建了bb_attachments gridfs对象。 所以默认情况下有两个集合,命名如下。 bb公司_附件.files, bb公司_附件.chunks在

当我用文件集合中的某个文件名查询mongoshell时,我得到如下的响应。。。在

> db.bb_attachments.files.find({"filename" : "C1208BSP130.pdf"}).toArray()
[
    {
            "_id" : "20120817014008229971__C1208BSP130.pdf",
            "contentType" : "application/pdf",
            "chunkSize" : 262144,
            "filename" : "C1208BSP130.pdf",
            "length" : 9177,
            "uploadDate" : ISODate("2012-08-17T01:40:08.253Z"),
            "md5" : "da39afb3968195f3ca5b8a1c25394b67"
    }
]
>

但是当我使用pythonidle查询它时,它会给出no file Exists异常,比如。。在

^{pr2}$

例外情况是。。在

>>> bb_attachments.get("20120817014008229971__C1208BSP130.pdf").read()

Traceback (most recent call last):
  File "<pyshell#50>", line 1, in <module>
    bb_attachments.get("20120817014008229971__C1208BSP130.pdf").read()
  File "C:\Python25\Lib\site-packages\gridfs\__init__.py", line 130, in get
    return GridOut(self.__collection, file_id)
  File "C:\Python25\Lib\site-packages\gridfs\grid_file.py", line 343, in __init__
    (files, file_id))
NoFile: no file in gridfs collection Collection(Database(Connection('my host', 27017), u'my db'), u'fs.files') with _id '20120817014008229971__C1208BSP130.pdf'
>>>

谁能详细解释一下吗。 为什么我会fs.文件而不是bb_附件。文件??在

NoFile: no file in gridfs collection Collection(Database(Connection('my host', 27017), u'my db'), **u'fs.files'**) with _id '20120817014008229971__C1208BSP130.pdf'

Tags: 文件noiniddb附件getpdf
1条回答
网友
1楼 · 发布于 2024-09-27 21:32:04

看起来您当前的bb_attachmentsGridFS对象已经创建 默认前缀为“fs”。在

尝试使用正确的前缀创建它:

bb_attachments = GridFS(db, collection="bb_attachments")

如果你跑了

^{pr2}$

它应该能够找到文件。在

相关问题 更多 >

    热门问题