Storing metadata in gridfs

I’m putting together a small document management application with Node.js and MongoDB. At this point I’m able to rather easily create documents in a collection I call documents (meaning electronic documents, not mongodb objects) which have a half dozen attributs. I’m also able to save files using GridFS, which creates fs.files and fs.chunks objects. What I’d really like to do is store the metadata directly on the gridfs fs.files objects (provided I still have access to all the search capabilities).

I can’t find any way to do this though. It seems I’m going to have to have the 2 separate collections and relate the 2 by having the Object IDs of the fs.files objects in the documents objects as a value.

Is there some way to have the metadata on the fs.files objects I’m just not seeing? Any links to anyone doing this would be helpful.

1 Like

Hi Mark,

The GridFS API is a convention for storing large binary data in MongoDB, but the underlying fs.files and fs.chunks collections are normal collections you can query and manipulate directly.

Custom metadata happens to be part of the GridFS API specification and the driver APIs have provision to include optional metadata which will be added to the fs.files document.

For example, if you are using the Node.js driver and openUploadStream:

`options.metadata` is an optional object to store in the file document's `metadata`  field.

For a more general use case description, see Metadata and Asset Management in the MongoDB Manual.

Regards,
Stennie

1 Like

can you show sample code how to retrieve metadata? i can upload metadata using UploadFromStream and filled GridFSUploadOptions but i cant find way how to download metadata, i can download file using DownloadToStreamByName