2

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

当我在文件集合中使用某些文件名查询 mongo shell 时,我得到如下响应...

> 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"
    }
]
>

但是当我使用 python IDLE 查询它时,它没有给我任何文件 Exists 异常,例如..

>>> bb_attachments.exists({"filename" : "C1208BSP130.pdf"})
False
>>>

和异常像..

>>> 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.files 而不是 bb_attachments.files?

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

1 回答 1

4

看起来您当前的bb_attachmentsGridFS 对象是使用默认前缀“fs”创建的。

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

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

现在如果你跑

bb_attachments.exists({"filename" : "C1208BSP130.pdf"})

它应该能够找到该文件。

于 2012-08-21T13:53:27.863 回答