Adding photos to users collection in mongodb - node.js

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.

Related

How to upload several images to Google Cloud Storage using Node.js

Looking for help to achieve the following points in GCS.
I want to be able to upload one object that is composed of two or more images to GCS.
All images, grouped in one object are uploaded at the same time. In other words as a bulk.
The object, composed of several files/images, has its own Id or a property that can be used to query the object.
Reading the GCS API docs, I found the method to upload one single image at a time. Also, reading similar questions in Stack Overflow, I have found how to upload several images at the same time but individually no as a bulk. However, I can not find a method provided by the API to group several images into an object and upload the object to GCS.
You can create a composite object in order to merge multiple images together in a single object - here is the documentation for this in Node.js
Nonetheless, composing objects requires that the source files are already stored in the same storage bucket and must have the same storage class;see document for More details.
Because of this, the composite object can only be created once all the files that you are willing for it to be composed of are stored in the same bucket.
For this reason, in case you would like to have this done prior to uploading to GCS, you should implement logic on node.js side like merging objects before uploading to gcs.
You can have a look at this document Node.js — How to Merge Objects.

How to store images and videos in mongoDB along with other form data

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.

Firebase fetching other user's Images efficiently

I have a firebase storage bucket set up for the primary purpose of storing user's profile pictures. Fetching the profile picture of the currentUser is simple, as I know the .uid. However, fetching the profile pictures for other users is not so straightforward as that first requires a query to my actual database (in this case a graph database) before I can even begin fetching their images. This process is aggravated by my backend having a three tier architecture.
So my current process is this:
get request to Node.js backend
Node.js queries graph database
Node.js sends data to frontend
frontend iteratively fetches profile pictures from other user's uid
What seems slow is the fact that my frontend has to wait for the other uids before it can even begin fetching the images. Is this unavoidable? Ideally, the images would be fetched concurrently with the info about the users.
The title here is Firebase fetching other user's Images efficiently but you're using a non-firebase database which makes it a little difficult.
The way I believe you could handle this in Firebase/Firestore would be to have duplicate data (pretty common with NoSQL databases).
Example:
Say you have a timeline feed, you probably wouldn't query the list of posts and then query user info from each of the posts. Instead, I would have a list of timeline posts for a given UID (the customer accessing the system right now), that list would include all the details needed to display the feed without another query. This could be users names, post description, and a link to their pictures based of a known bucket path to a bucket and directory structure and the UIDs. Something like gs://<my-bucket>/user-images/<a-uid>.jpg. Again, I don't have much exposure to graph databases so not sure how applicable the technique is there but I believe it could work the same.

Referring manually created collection stored in mongo atlas from node js

i am currently creating a nodejs app that deals with huge data. I created a table(.csv file) in excel and then converted it to to JSON file and added the document to MongoDB Atlas under the collection
players. The collection is huge, hence i did not create the collection inside from Node.JS. Now I want to refer to the fields inside the MongoDB collection from my Node.JS application and using it as a model..
how to solve this problem.....
You can just create a mongoose schema and model as you do normally. But, name the model player so that it points to your mongodb's players collection.

Storing images in MongoDB locally via script/json

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!

Resources