1

Hello friends I am new in nodejs and mongodb. Is it possible to upload multiple files inside single mongodb doc with other information.

1
  • If multiple file upload is not possible then how to design our doc structure Commented Jun 27, 2019 at 2:34

1 Answer 1

1

You can make use of BSON (Binary JSON) to store files in MongoDB collections. However, BSON has a limit of 16MB. If you are planning to store files bigger than that, consider GridFS

You can write files to MongoDB like so in Node.js:

var Binary = require(‘mongodb’).Binary;
//Read the file that you want to store
var file_data = fs.readFileSync(file_path);
var db_doc = {};
db_doc.file_data= Binary(file_data);

var my_collection = db.collection(‘files’);
my_collection.insert(db_doc, function(err, result){
//more code..
....
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks for your reply. But my question was how to store multiple files within same doc of mongodb with other user data

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.