Hi i need to store a file in mongodb using node.js, the file is placed in my desktop i will have to store it in my mongodb database.I came across something called gridfs but am not sure how to proceed further.Any help will be much appreciated
While I don't recommend storing big files in Mongo, though it's possible, smaller files would be better.
Simply read the file's text (if it's a text file), or binary (if it's in a binary format i.e executable). You can use the fs
library to read the file and encode it accordingly.
Then insert the data, stored in a variable, inside the database.
var fs = require('fs');
// Read file with proper encoding...
var data = //...
// Insert into Mongo
mongo.insert({file: data});
When you want to retrieve the file from the database, you'd do the opposite. The process of encoding/decoding is different depending on the type of file.