Nov 6, 2015

GridFS Index in MongoDB


GridFS Index
Unique and compound index used by GridFS on chunks collection for the files _id and n fields. _id field contains the _id of the chunk's "parent" document and the n filed contains the sequence number of the chunk, It's start with 0 for descriptions of the documents and fields in the chunks collection.

See following example:
cursor = db.fs.chunks.find({files_id: myFileID}).sort({n:1});
GridFS index allows efficient retrieval  of chunks using fields _id and n values.

Note: If your driver does not create this index, to fix this use the following operation

db.fs.chunks.createIndex( { files_id: 1, n: 1 }, { unique: true } );