Databases Reference
In-Depth Information
chunkSize
The size of each chunk comprising the file, in bytes. The default is 256K, but this
can be adjusted if needed.
uploadDate
A timestamp representing when this file was stored in GridFS.
md5
An md5 checksum of this file's contents, generated on the server side.
Of all of the required keys, perhaps the most interesting (or least self-explanatory) is
"md5" . The value for "md5" is generated by the MongoDB server using the filemd5 com-
mand, which computes the md5 checksum of the uploaded chunks. This means that
users can check the value of the "md5" key to ensure that a file was uploaded correctly.
When we understand the underlying GridFS specification, it becomes trivial to imple-
ment features that the driver we're using might not implement for us. For example, we
can use the distinct command to get a list of unique filenames stored in GridFS:
> db.fs.files.distinct("filename")
[ "foo.txt" ]
Server-Side Scripting
JavaScript can be executed on the server using the db.eval function. It can also be stored
in the database and is used in some database commands.
db.eval
db.eval is a function that allows you to execute arbitrary JavaScript on the MongoDB
server. It takes a string of JavaScript, sends it to MongoDB (which executes it), and
returns the result.
db.eval can be used to imitate multidocument transactions: db.eval locks the database,
executes the JavaScript, and unlocks the database. There's no built-in rollback, but this
does give you a guarantee of a series of operations occurring in a certain order (unless
an error occurs).
There are two options for sending code: enclosing it in a function or not. The following
two lines are equivalent:
> db.eval("return 1;")
1
> db.eval("function() { return 1; }")
1
Defining an enclosing function is necessary only if you are passing in arguments. These
can be passed using db.eval 's second argument, which is an array of values. For
example, if we wanted to pass the username as an argument to a function, we could
say the following:
 
Search WWH ::




Custom Search