Database Reference
In-Depth Information
Many people have commented in connection with this issue that deleting multiple files with the same name is
not a problem because no application would have duplicate names. This is simply not true; and in many cases, it doesn't
even make sense to enforce unique names. For example, if your app lets users upload photos to their profiles, there's a
good chance that half the files you receive will be called photo.jpg or me.png .
Note
Of course, if you are unlikely to use mongofiles to manage your live data—and in truth no one ever expected it to be
used that way—then you just need to be careful when deleting data in general.
Retrieving Files from MongoDB
So far, you haven't actually pulled any files out from MongoDB. The most important feature of any database is that
it lets you find and retrieve data once it's been put in. The following snippet retrieves a file from MongoDB using the
mongofiles command get :
$ mongofiles get /tmp/dictionary
connected to: 127.0.0.1
done write to: /tmp/dictionary
$
This example includes an intentional mistake. Because it specifies the full name and path of the file you want to
retrieve (as required), mongofiles writes the data to a file with the same name and path. Effectively, this overwrites
the original dictionary file! This isn't exactly a great loss, because it is being overwritten by the same file—and the
dictionary file was only a temporary copy in the first place. Nevertheless, this behavior could give you a rather nasty
shock if you accidentally erase two weeks of work. Trust us, you won't figure out where all your work went until
sometime after the event! As when using the delete command, you need to be careful when using the get command.
Summing Up mongofiles
The mongofiles utility is a useful tool for quickly looking at what's in your database. If you've written some software,
and you suspect something might be amiss with it, then you can use mongofiles to double-check what's going on.
It's an extremely simple implementation, so it doesn't require any fancy logic that could complicate
accomplishing the task at hand. Whether you would use mongofiles in a production environment is a matter of
personal taste. It's not exactly a Swiss army knife; however, it does provide a useful set of commands that you'll
be grateful to have if your application begins misbehaving. In short, you should be familiar with this tool because
someday it might be exactly the tool you require to solve an otherwise nettlesome problem.
Exploiting the Power of Python
At this point, you have a solid idea of how GridFS works. Next, you will learn how to access GridFS from Python.
Chapter 2 covered how to install PyMongo; if you have any trouble with the examples, please refer back to Chapter 2
and make sure everything is installed correctly.
If you've been following along with the previous examples in this chapter, you should now have one file in
GridFS. You'll also recall that the file is a dictionary file, so it contains a list of words. In this section, you will learn how
to write a simple Python script that prints out all the words in the dictionary file. Sure, it would be simpler and more
efficient to simply cat the original file—but where would the fun be in that?
 
 
Search WWH ::




Custom Search