Database Reference
In-Depth Information
This command copies all the contents of the file to a nice and simple path that you can use easily. Of course, you
can use any file that you wish for this example; it doesn't need to be any particular size or type.
Rather than describe all the options you can use with
mongofiles
, let's jump right in and start playing with some
of the tool's features. This topic assumes that you're running
mongofiles
on the same machine as MongoDB. If you're
not, then you'll need to use the
-h
option to specify the host that MongoDB is running on. You'll learn about the other
options available in the
mongofiles
command after putting it through its paces.
First, let's list all the files in the database. We're not expecting any files to be in there yet, but let's make sure. The
list
command lists the files in the database so far:
$ mongofiles list
connected to: 127.0.0.1
$
OK, so that probably wasn't very exciting. Keep in mind that
mongofiles
is a proof-of-concept tool; it's probably
not a tool you will use much with your own applications. However,
mongofiles
is great for learning and testing. Once
you create a file, you can use the tool to explore the files and chunks that are created.
Let's kick things up a notch and the
put
command to add the dictionary file created previously (remember: you
can use any file that you like for this example):
$ mongofiles put /tmp/dictionary
connected to: 127.0.0.1
added file: { _id: ObjectId('51cb61b26487b3d8ce7af440'), filename: "/tmp/dictionary", chunkSize:
262144, uploadDate: new Date(1372283314621), md5: "40c0825855792bd20e8a2d515fe9c3e3", length:
4953699 }}}
done!
$
This example returns some useful information; however, let's double-check the information it shows by
confirming that the file is there. Do so by rerunning the
list
command:
$ mongofiles list
connected to: 127.0.0.1
/tmp/dictionary 4953699
$
This example shows the dictionary file, along with its size. The information clearly comes from the
files
collection, but we're getting ahead of ourselves. Let's take a moment to step back and examine the output returned
from the
put
command in this example.
Using the _id Key
As you know, each document in MongoDB includes a unique identifier stored in the
_id
key. Like MySQL's
auto_increment
field, the
_id
key is not of much direct interest, apart from the fact that it allows you to pick out a
specific file.
Working with Filenames
The output from the
put
command also shows a
Filename
key, which itself needs a little explanation. Generally, you
will want to keep this field unique to help prevent major confusion; however, that's not entirely necessary. In fact,
if you run the
put
command again, you'll end up with two documents that look identical. In this case, the files and