ultimate_beaver
ultimate_beaver

Reputation: 85

How to save a file in mongoose

I want to save a file using node JS to save a file in an attribute using mongoose schema to save an entry in mongoose collection.

What should be the type of the attribute in the schema?
And how can I save the file to this attribute?

I used the regular save function, but it saved all the attributes posted by the router except this one attribute of the image

Upvotes: 0

Views: 5474

Answers (2)

Artiphishle
Artiphishle

Reputation: 886

Here the Mongoose way: https://mongoosejs.com/docs/schematypes.html#buffers

I dislike keeping resources in sync (filesystem & Db Record), at some point (usually in prod) there will be records without files or vice versa. I think the file should be where it belongs context-wise, whenever possible architecture-wise.

MongoDB/Mongoose is perfectly ready (maybe 2017 it wasn’t 🤷‍♂️

Heavy files (16Mb+) should he stored using GridFs.

Upvotes: 2

Ronny vdb
Ronny vdb

Reputation: 2474

You should not save a file in mongoose, instead you should upload the file, and save the path to the file in mongoose, for this you will just need a string field to save the path

Upvotes: 2

Related Questions