Database Reference
In-Depth Information
Accessing the Words
In its original implementation, the PyMongo driver used a file-like interface to leverage
GridFS. This is somewhat different from what you saw in this chapter's earlier examples
with mongofiles , which were more FTP-like in nature. In the original implementation of
PyMongo, you could read and write data just as you do for a normal file.
This made PyMongo very much like Python to use, and it allowed for easy
integration with existing scripts. However, this behavior was changed in version 1.6 of the
driver, and this functionality is no longer supported. While very Python-like, the behavior
had some problems that made the tool less effective overall.
Generally speaking, the PyMongo driver attempts to make GridFS files look and
feel like ordinary files on the filesystem. On the one hand, this is nice because it means
there's no learning curve, and the driver is usable with any method that requires a file. On
the other hand, this approach is somewhat limiting and doesn't give a good feel for how
powerful GridFS is. Important changes were made to how PyMongo works in version 1.6,
particularly in how get and put work.
This revised version of pyMongo isn't too dissimilar from previous versions of the
tool, and many people who used the previous apI have found it easy to adapt to the revised
version. That said, Mike's changes haven't gone down well with everybody. For example,
some people found the file-based keying in the old apI to be extremely useful and easy to
use. The revised version of pyMongo supports the ability to create filenames, so the missing
behavior can be replicated in the revised version; however, doing so does require a bit
more code.
Note
Putting Files into MongoDB
Getting files into GridFS through PyMongo is straightforward and intentionally similar
to the way you do so using command-line tools. MongoDB is all about throughput, and
the changes to the API in the revised version of PyMongo reflect this. Not only do you get
better performance, but the changes also bring the Python driver in line with the other
GridFS implementations.
Let's put the dictionary into GridFS (again):
>>> with open("/tmp/dictionary") as dictionary:
... uid = fs.put(dictionary)
...
>>> uid
ObjectId('51cb65be2f50332093f67b98') >>>
 
 
Search WWH ::




Custom Search