What is GridFS in MongoDB?
Why Interviewers Ask This
Foundational questions like this help interviewers calibrate the rest of the interview. A confident, accurate answer signals that you have solid MongoDB basics — a prerequisite for any developer role.
Answer
GridFS is MongoDB's specification for storing and retrieving files that exceed the BSON document size limit of 16MB. It divides files into smaller chunks (default 255KB each) and stores them in two collections: fs.files (file metadata: filename, uploadDate, length, chunkSize, contentType, custom metadata) and fs.chunks (file data chunks). When to use GridFS: (1) Files larger than 16MB; (2) You want to access sections of a large file without loading it all into memory (random reads); (3) You want to store file metadata alongside the file in MongoDB; (4) You want to replicate files across replica set members automatically. When NOT to use GridFS: (1) Files are under 16MB — just store as binary BSON; (2) You need high-performance CDN serving — S3 + CloudFront is much better; (3) You need very frequent updates to files — GridFS stores files as immutable chunks. GridFS vs S3: S3 is almost always preferable for file storage in production — much cheaper, scales infinitely, integrates with CDNs, has pre-signed URL support. Use GridFS only if you're already using MongoDB and want to avoid another infrastructure dependency. Driver usage: all MongoDB drivers support GridFS: const bucket = new GridFSBucket(db); fs.createReadStream("large.mp4").pipe(bucket.openUploadStream("large.mp4")).
Common Mistake
A common mistake is memorizing definitions without understanding implications. When asked this question, go one level deeper — explain what happens when this concept is misused or ignored.