使用NOSQL和MongoDB,将任何类型的文件存储在数据库中的最佳方法是什么

发布于 2025-01-30 01:17:58 字数 167 浏览 2 评论 0原文

因此,我正在创建一个票务系统,我想做的就是将任何类型的文件添加到我的机票中,然后将其存储在数据库中。 因此,我正在考虑仅将NOSQL数据库用于“文件部分”。 但是,设置我的数据库的最佳方法是什么? 例如,如果我必须执行数据库架构,那么数据库中必须有哪些字段才能存储我想要的所有文件(PDF,JPG,JPEG .....)

So I'm creating a ticket system, and what I'd like to do is to add any type of file to my tickets and store them in my database.
So I was thinking about using a noSQL database just for the "file part".
But what would be the best way to set up my database?
For example if I have to do a database schema, what are the fields that would have to be in the database, to be able to store all the files I want (PDF, JPG, JPEG.....)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

ゝ偶尔ゞ 2025-02-06 01:17:58

您可以将文件嵌入MongoDB中的JSON文档中,其中已知限制为16MB。
对于16MB以上的文件,您可以使用 gridfs 规范。
但总的来说,最佳实践方法是将参考存储在MongoDB JSON文档中,并将文件存储在外部专用文件对象存储中,例如S3 ...

You can embed files inside json document in mongoDB with known limit of 16MB.
For files above 16MB you can use the gridFS specification.
But in general the best practice approach is to store references in the mongoDB JSON document and store the files in external specialized file object storage like S3 ...

救星 2025-02-06 01:17:58

如果要在16MB以上上载文件%20 for,AS%20many%20files%20AS%20。” rel =“ nofollow noreferrer”> gridfs 。

方法1

除了要存储诸如PDF之类的文件或图像之外,您可以使用 base64 strings 。然后,您可以将文件存储在数据库中。

请在下面找到我的代码,然后在此处尝试。您可以找出如何生成base64字符串以及如何在浏览器中查看它。(如果此代码在此处不起作用(PDF部分),请复制代码并在本地计算机上运行)

<!DOCTYPE html>
<html>

<head>
    <title>Convert to Base64</title>
    <script src="https://code.jquery.com/jquery-1.9.1.min.js"></script>
</head>

<body>
    <H1>Select file to upload and convert into base64 string</H1>
    <hr>
    <br>
    <label for="fileUpload">Choose file:</label>
    <input type="file" id="fileUpload" name="fileUpload" />
    <br><br>
    <button id="jsonConvert">Convert into Base64</button>
    <img style='display:block; width:100px;height:100px;' id='base64image' src='' />
    <iframe id='base64pdf' width='100%' height='100%' src=''>

    </iframe>
</body>

</html>
<script type="text/javascript">
    const toBase64 = file => new Promise((resolve, reject) => {
        const reader = new FileReader();
        reader.readAsDataURL(file);
        reader.onload = () => resolve(reader.result);
        reader.onerror = error => reject(error);
    });

    $('#jsonConvert').click(function () {
        const uploadedFile = document.querySelector('#fileUpload').files[0];
        toBase64(uploadedFile)
            .then(res => {
                const filetype = res.split("/")[0];
                if (filetype == "data:application") {
                    document.getElementById('base64pdf').src = res;
                } else if (filetype == "data:image") {
                    document.getElementById('base64image').src = res;
                }

                console.log(res);
            })
            .catch(err => {
                console.log(err);
            })
    });
</script>

方法2

您可以将文件上传到像S3存储桶这样的储物桶中,并获取其链接并将该链接存储在数据库中。使用该链接可以查看。

希望您可以从上述方法找到解决方案。

If you want to upload files above 16MB you can use gridFs.

METHOD 1

Other than that if you want to store file like PDF or Images you can use base64 strings. Then you can store your file as a string in the database.

Please find my code below and try it here. You can find out how to generate the base64 string and how we can view it in our browser.(If this code is not working here (PDF part), please copy the code and run on your local machine)

<!DOCTYPE html>
<html>

<head>
    <title>Convert to Base64</title>
    <script src="https://code.jquery.com/jquery-1.9.1.min.js"></script>
</head>

<body>
    <H1>Select file to upload and convert into base64 string</H1>
    <hr>
    <br>
    <label for="fileUpload">Choose file:</label>
    <input type="file" id="fileUpload" name="fileUpload" />
    <br><br>
    <button id="jsonConvert">Convert into Base64</button>
    <img style='display:block; width:100px;height:100px;' id='base64image' src='' />
    <iframe id='base64pdf' width='100%' height='100%' src=''>

    </iframe>
</body>

</html>
<script type="text/javascript">
    const toBase64 = file => new Promise((resolve, reject) => {
        const reader = new FileReader();
        reader.readAsDataURL(file);
        reader.onload = () => resolve(reader.result);
        reader.onerror = error => reject(error);
    });

    $('#jsonConvert').click(function () {
        const uploadedFile = document.querySelector('#fileUpload').files[0];
        toBase64(uploadedFile)
            .then(res => {
                const filetype = res.split("/")[0];
                if (filetype == "data:application") {
                    document.getElementById('base64pdf').src = res;
                } else if (filetype == "data:image") {
                    document.getElementById('base64image').src = res;
                }

                console.log(res);
            })
            .catch(err => {
                console.log(err);
            })
    });
</script>

METHOD 2

You can upload your file to a storage bucket like S3 bucket and get the link of it and store that link in your database. Using that link you can view it.

Hope you will find a solution from above methods.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文