How to perform Update operations in GridFS (using Java)?

前端 未结 1 1206
独厮守ぢ
独厮守ぢ 2020-12-11 08:50

I am using Mongo-Java-Driver 2.13 I stored a PDF file (size 30mb) in GridFS. I am able to perform insertion, deletion and find operati

相关标签:
1条回答
  • 2020-12-11 09:17

    In GridFS you are not removing/deleting a single document but actually a bunch of documents (files are split into chunks and each chunk is a separate document). That means replacing a file is simply not possible in an atomic manner.

    What you can do instead is:

    1. insert a new file with a new name
    2. after this happened (use the replica acknowledged write-concern), update all references to the old file to point to the new one
    3. after you got a confirmation for this, you can delete the old file

    GridFS is kind of a hackish feature. It is often better to just use a separate fileserver with a real filesystem to store the file content and only store the metadata in MongoDB.

    0 讨论(0)
提交回复
热议问题