How to store filename in MongoDB

307 views
Skip to first unread message

KingCanadian

unread,
Apr 13, 2012, 11:33:28 AM4/13/12
to mongodb-user
Hello everybody,

i store files in MongoDB as a subarray in a document.
The key is the filename - the value is the binary data.

array(document
array(filename => filedata)
)

Because it are small files (some KB per file) i dont use gridFS.

Now i want to update some fields an MongoDB says: uncaught exception:
can't have . in field names

So i should not use the filename as key.

What would you do?

Replace the dot with an other character before storing and replace
back when reading? Are there other characters which are not allowed?

Or should i save the name in an other sub-array as a value?
e.g.
array(document
array(id=>array(array('filename'=>filename),array('filedata' =>
filedata))
)

Regards
Juergen

Glenn Maynard

unread,
Apr 13, 2012, 11:47:47 AM4/13/12
to mongod...@googlegroups.com
On Fri, Apr 13, 2012 at 10:33 AM, KingCanadian <juerge...@domino-verlag.de> wrote:
Replace the dot with an other character before storing and replace
back when reading? Are there other characters which are not allowed?

You need to escape "." and "$".  It's a common problem when using email addresses and non-integer numbers as keys.

Or should i save the name in an other sub-array as a value?
e.g.
array(document
array(id=>array(array('filename'=>filename),array('filedata' =>
filedata))

I can't read this, but yes, you can also change your data structure around to store the filename as a value inside the document instead of as a document key.

{ 'filename.txt': { data: 'text' } }
->
[ { filename: 'filename.txt', data: 'text' }, ... }

This is usually inconvenient when you really want a dictionary, though.

--
Glenn Maynard

Marc

unread,
Apr 13, 2012, 12:10:08 PM4/13/12
to mongodb-user
The only illegal characters in key names are "." and the first
character may not be "$". This is documented on the "Legal Key Names"
page:
http://www.mongodb.org/display/DOCS/Legal+Key+Names

Replacing the dot with a valid character is an acceptable solution.

Alternatively, you could rearrange your document structure, such that
each document contains two Keys; "filename", and "Binary_Data". I
believe that this is similar to your second proposed solution. In
general, it is more common for documents to contain the same keys.
This makes querying a little more straightforward.

For example, given the two following document structures:

{_id:1, "filename":"path/to/myFile", "Binary_Data":"100100..."}

{_id:2, "path/to/myFile" : "100100..."}

The first document (_id:1) would seem a little easier to write queries
for if the name of the file is known:

> db.files.find({"filename":"path/to/myFile"}

For the second document, the binary data would have to be known, or
another field would have to be matched on. Alternatively, the
"$exists" operator could be used.

> db.files.find({"path/to/myFile":{$exists:true}})

The $exists operator is explained here:
http://www.mongodb.org/display/DOCS/Advanced+Queries#AdvancedQueries-%24exists

To me, the first document structure seems a little more
straightforward. Furthermore, an index may be created on the
"filename" key. Index creation is not really possible for the second
document, because each will have a different key name.

The most generic advice is to try it both ways, and experimentally
determine which method is best for your application.

KingCanadian

unread,
Apr 13, 2012, 2:31:45 PM4/13/12
to mongodb-user


On 13 Apr., 17:47, Glenn Maynard <gl...@zewt.org> wrote:
> You need to escape "." and "$".  It's a common problem when using email
> addresses and non-integer numbers as keys.
>

If it is a common problem. Should there not be an solution out of the
box for?
Escaping the "." and "$" means to replace them - right?


> I can't read this, but yes, you can also change your data structure around
> to store the filename as a value inside the document instead of as a
> document key.
>
> { 'filename.txt': { data: 'text' } }
> ->
> [ { filename: 'filename.txt', data: 'text' }, ... }

Yes you are right - this is what i tried to suggest.

KingCanadian

unread,
Apr 13, 2012, 2:35:06 PM4/13/12
to mongodb-user
Dear Glenn and Marc,

thank you both for your advices and suggestions!
Perhaps i should think again about the database design and alter the
structure as suggested.

Regards
Juergen
Reply all
Reply to author
Forward
0 new messages