I am building a project using the MEVN stack. The website is a game seller web, and when I had to store the images in the Mongo database, I realised that it was a little bit more difficult than I thought. I finally used multer to store the images but I am only able to store them using POSTMAN and its file upload functionality. My problem is the customer wants a local demo with already stored data in the database, without using any external program. I've worked with Mongo and I know it is possible to charge the DB data using a JSON file or even a JSON variable into the DB script, but I don't know if it is possible to include the image 'originalName' from a image already stored in the /uploads folder.
This is my game model:
MongoDB game model
Here is the DB object and the way the image is stored (Then I use the originalName to get it via relative paths):
MongoDB Compass DB object
And finally here is my post method (Currently I'm using POSTMAN to populate the data and it is tedious):
POST game method
Please I need a way to store this 'image name' into my DB at the beginning of the first compilation of the project. I don't even know if I have to store the images first and then export the project with this base images to populate the database, or there is an easier way to do it. Thank you very much!
Related
I'm new to mongoDB,
I want to fill a form for a post model and one of the fields is file-type (image or video), can I store the attached media in the same mongo document ? If not, how should I go about doing this and is there a helpful guide I can follow ?
this is how my backend looks like
this is my Post model that I'm going to fill it in the form
NB : I'm using angular in my front-end
If the media is relatively small like an 8K thumbnail then you can store it as a binary type field as a peer to the rest of your ints, strings, and dates.
However, an individual document cannot be larger than 16MB so such an approach in general is not feasible especially for video content.
You can use the gridFs utils which are bundled with the client side drivers. A comprehensive example is posted here: How to save images from url into mongodb using python?
If you do not require the media to be managed by the database storage subsystem, a practical alternative is to store the media in an AWS S3 bucket (or Azure or GCP equivalent) and store just the path to the content in mongodb.
I am new to using mongodb, I have a collection of users, I want every user to upload some of their photos. So, I am thinking of creating property called images for every user document. this would be an array of images. However, I don't know if it's applicable or not, or how to do so. Any ideas? I am using node.js
There multiple libraries that are created to help store images in a MongoDB database. You can see some at this question: Store images in a MongoDB database
Also, for good design, you should consider having the images property containing IDs that correspond to the image, instead of the actual images themselves; it would be named imageIds.
I can't find a response to my question.
I'm building a React app using NodeJS and CRA and i need to implement an uploading avatar system. But i'm not sure where to save the uploaded image. My Node server serve a static folder 'public', so does i need to save images in /public/avatar? But each time i will make update on the app and re-build the client-side folder, this will overwrite the public folder and remove all the previous uploaded avatar ? I'm right ? So what are you suggesting me ?
Thanks,
There are multiple locations that you can store your user uploaded images, though storing them in your public is probably not the best location.
In the case where you were using a database like MongoDB, you could store the image inside Mongo using gridfs and serve the data using a route when you retrieve the user information. Similarly, you can also store in the database a path to the file, and return the path, or the file data, from the route as well.
Be careful with user uploads, however, as arbitrarily allowing uploaded data can lead to unanticipated results if you're not careful.
You could also use Gravatar (https://gravatar.com/).
User can choose an avatar assigned to their mail address hash, or use an automatically generated one by default.
Though with this solution you cannot let users change their avatar directly on your website.
It is widely used on well known websites like StackOverflow.
I am working on a small project and I can upload data to a mongo database but so far I have been unable to save an image to the same database, a friend of mine advised me to send a reference to the database but at this stage I have not worked out how to do this, any assistance would be greatly appreciated.
You can use node-s3-uploader library from Turistforeningen or build something like that if you want to save on local disk. The work behind is get the image file which you received, transform it to multiple versions to optimize bandwidth, you keep a reference to original version on MongoDB or MySQL.
When client request an image, send them the original link, depend on which case they need, client will deduce original link to re-scale link.
Assets like image should not be saved to database system directly but to local disk of your server, and you just save the link of that image to the database.
The database system like mongodb or mysql should save things that will be queried, and that's why we use database system. Things like binary file or image should just save to the local disk, because the content of these file is unreadable, and also can not be queried. But the name, path or URL of these file can be queried, so we normally save these things to database system.
I am trying to upload an image to my server through a node.js server using express. I am in the process of writing an CRUD API but I am stuck on how to POST and save the image in a directory on my server.
//post
app.post('/public/media', function(req,res){
});
This is the barebones of my post method. I am trying to store the image in my media file. How should I go about this?
Note, I am not trying to store the image into a database. Rather, I am trying to store the image in a folder on my server and simply store the path to the image in my database.
Without duplicating an entire article about this issue, checkout this tutorial out. You will need to make slight adjustments to this code if using Express 4, otherwise it will work great.
Comment if you have issues below.