Explain Like I'm Five: Form w/ Text and Image Field > Routes > Controller > Write to MongoDB Document - GridFS goes where?

浪尽此生 提交于 2021-02-11 16:28:52

问题


I have been trying to read the documentation for GridFS and MongoDB for awhile. They just keep repeating the same thing and I can't make sense of it.

Desired Output: The user submits a form that form contains many fields, but one is an image. The request needs to store the data in a collection and make a new document, which can be retrieved later. My main question is how do I use GridFS in this situation to store an image in that document.

It says GridFS makes two collections in my database files and chunks. So how do those collections relate to my other collection which has the other form data?

I assume a reference to these files and chucks collection, however, I can't make any sense of this. It's been a few days and I feel like it's time to reach out to my StackOverflow community.

Can someone please explain to me the program flow and key points for how I can achieve my goal of storing an image in a document using gridfs?

GridFS is mentioned everywhere and seems to be popular but I can't make sense of it. These moments of utter confusion usually result in a breakthrough, so I'm eager to learn from veterans and experts.


回答1:


GridFS collections are internal implementation detail of GridFS. There is no linking between them and your other data.

To write to and read from GridFS, you would use GridFS APIs provided by your driver. Generally this means that, if you are saving for example some fields and a binary blob like an image, you would perform the save in two steps (one insert/update operation for the fields and a separate GridFS operation for the binary blob).

Can someone please explain to me the program flow and key points for how I can achieve my goal of storing an image in a document using gridfs?

You wouldn't store the image in your document. You would store the image in GridFS, and in your document you could include a reference to the GridFS file (those have their own ids).



来源:https://stackoverflow.com/questions/63960537/explain-like-im-five-form-w-text-and-image-field-routes-controller-writ

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!