Pregunta

Estoy jugando con MongoDB (Gridfs) para almacenar archivos (ZIP) y tratar de recuperarlos usando "Pymongo" de Python ', pero no funciona, como se esperaba, no puedo entender cómo recuperar los archivos (s)He añadido ...

a continuación es el código I corrió desde inactivo (Python 3.4.1)

>>> db = Connection(port=31000, host="localhost").fs
>>> db.name
'fs'
>>> db.validate_collection
<bound method Database.validate_collection of Database(Connection('localhost', 31000), 'fs')>
>>> blob_store = gridfs.GridFS(db, collection='bstore')
>>> local_db = dict()
>>> k = r'd:\test\my-scripts.zip'
>>> local_db[k] = blob_store.put(open(k, 'rb'))

[** File is saved, i checked using robomongo **]

>>> blob_store.exists(filename=k)
False
>>> blob_store.exists("53da7cb1b3b44b13e0e27721")
False
>>> local_db
{'d:\\test\\my-scripts.zip': ObjectId('53da7cb1b3b44b13e0e27721')}
>>> blob_store.list()
[]
>>> b = gridfs.GridFS(db, collection='bstore.files')
>>> b.list()
[]
>>> x = blob_store.get(Objectid("53da7cb1b3b44b13e0e27721"))
Traceback (most recent call last):
 File "<pyshell#20>", line 1, in <module>
 x = blob_store.get(Objectid("53da7cb1b3b44b13e0e27721"))
NameError: name 'Objectid' is not defined
>>> local_db
{'d:\\test\\my-scripts.zip': ObjectId('53da7fd0b3b44b13e0e2772a')}
>>> blob_store.find()
<gridfs.grid_file.GridOutCursor object at 0x0000000003DC1828>
>>> a = blob_store.find(ObjectId("53da7fd0b3b44b13e0e2772a"))
Traceback (most recent call last):
   File "<pyshell#29>", line 1, in <module>
   a = blob_store.find(ObjectId("53da7fd0b3b44b13e0e2772a"))
NameError: name 'ObjectId' is not defined

Ahora no estoy seguro de cómo recuperar el archivo de Mongo?¿Falta algo obvio?

gracias

¿Fue útil?

Solución

Necesitas importar el símbolo de ObjectId:

from bson import ObjectId

Entonces su código debería funcionar.

Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top