简体   繁体   中英

Add attachment in MongoDB document field

I am looking for a solution to add attachment in a MongoDB document. For example, suppose I have a document like this in MongoDB,

[
    {
        "_id": "5e3bff8d1c9d4400009b85be",
        "title": "Post 1 Title",
        "author": "Author 1"
    }
]

I want to attach a file in this same document. I have a field in the front end form to select the file to attach. I went thru GridFS documentation and I got to know that it can be used to store attachments in MongoDB. But I am little lost in figuring out how to attach/delete that file in the document. I am new to MongoDB so Hope I am making some sense here..

Any help on this is very much appreciated

Thanks

I think you should not store "documents and files in mongodb" because it will make your database heavy and can slow the query speed. If you want to store files or document then you can use aws s3 or your local machine and the name of file or document you can store into mongodb. Your document will look some what like this.

[
    {
        "_id": "5e3bff8d1c9d4400009b85be",
        "title": "Post 1 Title",
        "author": "Author 1",
        "file": "image.png"
    }
]

Now using any language fetch the name of the file and open it.

Note In real world mostly files are stored in this way.

Thank You

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM