Asim Altaf
Asim Altaf

Reputation: 11

How to Update MongoDB GridFS Chunk Data in Go?

Hey Stack Overflow community! I'm facing a challenge with updating the data field of files stored in my MongoDB using GridFS. I'm not quite sure how to approach it, and I could use some guidance.

Here's the setup:

My main database is named node-craft. It contains three collections: node, Node-FileStorage.files, and Node-FileStorage.chunks. Below are sample objects from each of these collections for reference: node

  1. node
{
  "_id": {
    "$oid": "6526a1497d7b83889296939e"
  },
  "id": "2705973598",
  "name": "man_secretpassword.txt",
  "type": 1,
  "parentId": "2705952637",
  "directory": "/PROJECT/man_secretpassword.txt",
  "files": {
    "$oid": "6526a1497d7b83889296939c"
  }
}
  1. Node-FileStorage.files
{
  "_id": {
    "$oid": "6526a1497d7b83889296939c"
  },
  "length": {
    "$numberLong": "27"
  },
  "chunkSize": 261120,
  "uploadDate": {
    "$date": "2023-10-11T13:21:13.309Z"
  },
  "filename": "man_secretpassword.txt"
}
  1. Node-FileStorage.chunks
{
  "_id": {
    "$oid": "6526a1497d7b83889296939d"
  },
  "files_id": {
    "$oid": "6526a1497d7b83889296939c"
  },
  "n": 0,
  "data": {
    "$binary": {
      "base64": "bWFuQGdtYWlsLmNvbSAxMjM0IDAzMDkzMg0K",
      "subType": "00"
    }
  }
}

**I've attempted various approaches, but none seem to work. I have a string with the newContent, and I also have the files_id (as seen in the chunks collection). My goal is to update the data of the chunk that corresponds to this files_id and then return the updated data.

Here's my current update function, but it's not functioning as expected:**

So I tried everything but nothing is working. I have string that has the newContent and the files_id (If you look at the chunks collection). I want to update the Data of the chunk that has the files_id and then return. Can someone help me out?

My Current Update Function (It does nothing):

func UpdateGridFSChunk(chunkID string, newContent string) error {
    // Convert the new content string to bytes.
    newData := []byte(newContent)

    // Find the chunk by its ID.
    chunkObjID, err := primitive.ObjectIDFromHex(chunkID)
    if err != nil {
        return err
    }

    filter := bson.M{"_id": chunkObjID}
    chunkFileCursor, err := fs.Find(filter)
    if err != nil {
        return err
    }

    if chunkFileCursor.Next(context.Background()) {
        // Get the GridFS file properties.
        chunkFile := chunkFileCursor.File()

        // Create a new ObjectID for the updated chunk.
        newChunkObjID := primitive.NewObjectID()

        // Replace the chunk's data with the new data.
        err = fs.UploadFromStreamWithID(newChunkObjID, chunkFile.Filename, bytes.NewReader(newData))
        if err != nil {
            return err
        }

        err = fs.Delete(chunkObjID)
        if err != nil {
            return err
        }
    }

    return nil
}

Upvotes: 1

Views: 100

Answers (0)

Related Questions