Database Reference
In-Depth Information
That's it! The preceding snippet returns a file-like object; thus, you can print all the words in the dictionary using
the following snippet:
>>> for word in new_dictionary:
... print word
Now watch in awe as a list of words quickly scrolls up the screen! Okay, so this isn't exactly rocket science.
However, the fact that it isn't rocket science or in any way difficult is part of the beauty of GridFS—it does work as
advertised, and it does so in an intuitive and easily understood way!
Deleting Files
Deleting a file is also easy. All you have to do is call fs.delete() and pass the _id of the file, as in the following
example:
>>> fs.delete(uid)
>>> new_dictionary = fs.get(uid)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/usr/lib/python2.6/site-packages/pymongo-2.5.2-py2.6-linux-x86_64.egg/gridfs/__init__.py",
line 140, in get
return GridOut(self.__collection, file_id)
File "/usr/lib/python2.6/site-packages/pymongo-2.5.2-py2.6-linux-x86_64.egg/gridfs/grid_file.py",
line 392, in __init__
(files, file_id))
gridfs.errors.NoFile: no file in gridfs collection Collection(Database(Connection('localhost',
27017), u'test'), u'fs.files') with _id ObjectId('51cb65be2f50332093f67b98') >>>
These results could look a bit scary, but they are just PyMongo's way of saying that it couldn't find the file. This
isn't surprising, because you just deleted it!
Summary
In this chapter, you undertook a fast-paced tour of GridFS. You learned what GridFS is, how it fits together with
MongoDB, and how to use its basic syntax. This chapter didn't explore GridFS in great depth, but in the next chapter,
you'll learn how to integrate GridFS with a real application using PHP. For now, it's enough to understand how GridFS
can save you time and hassle when storing files and other large pieces of data.
In the next chapter, you'll start putting what you've learned to real use—specifically, you'll learn how to build a
fully functional address book!
 
Search WWH ::




Custom Search