Reputation: 53
Is there a way to upload files directly to a mongodb schema
?
I'm currently developing an API
. There's a form that consists of multiple fields including 3 different files (as in PDFs) but I haven't found a way to store them in the schema so it's fast to access.
I think the schema should look something like this:
const SomeSchema = mongoose.schema({
data: {
type: String,
required: true
},
data:{
type: Date,
required: true
}
data:{
type: String,
required: true
}
file:
type: File(?),
required: true
)}
But I don't think this is correct since I have watched some tutorials and they don't store files in the same collection. Is it not possible?
Upvotes: 0
Views: 955
Reputation: 58
You can store the binary data of the file as Buffer in your document as Seyyed said. For that you have to edit the file field on your mongoose schema as shown:
file:{
data: Buffer,
contentType: String
}
Or else we can upload the file to the uploads folder in your project directory using multer and you can save the path to the file on you database. You can use this link as your reference https://www.section.io/engineering-education/uploading-files-using-multer-nodejs/
Upvotes: 1