[CSHARP-2056] How to update GridFS metadata after the file has been uploaded Created: 06/Oct/17  Updated: 27/Oct/23  Resolved: 06/Oct/17

Status: Closed
Project: C# Driver
Component/s: GridFS
Affects Version/s: 2.4.4
Fix Version/s: None

Type: Task Priority: Minor - P4
Reporter: Nagendra [X] Assignee: Robert Stam
Resolution: Works as Designed Votes: 0
Labels: None
Remaining Estimate: Not Specified
Time Spent: Not Specified
Original Estimate: Not Specified
Environment:

Mongo DB With C#


Issue Links:
Duplicate
is duplicated by CSHARP-2057 how to update the gridfs metadata Closed

 Description   

I want To Try to update docfiles.file Collection with In Meta data .It Is Posible or Not Give me Exampleeee



 Comments   
Comment by Nagendra [X] [ 09/Oct/17 ]

This only Direct Filed Update I know this thing But I want to update with on Meta only Any Provision is thier

Comment by Robert Stam [ 06/Oct/17 ]

The GridFS API lets you provide custom metadata when the file is first uploaded. But the GridFS API does not provide any way to update the metadata after the file has been uploaded.

However, the metadata is stored in the fs.files collection (note: replace fs with your bucket name if you used a custom bucket name), which is a collection like any other. The contents of the fs.files collection are described in the definition of "Files collection document" here:

https://github.com/mongodb/specifications/blob/master/source/gridfs/gridfs-spec.rst#terms

As long as you are careful not to disturb the other fields you can update the metadata directly using a regular update statement.

Something like this:

var filesCollection = database.GetCollection<BsonDocument>("fs.files");
var filter = Builders<BsonDocument>.Filter.Eq("_id", fileId);
var update = Builders<BsonDocument>.Update.Set("metadata", metadata);
filesCollection.UpdateOne(filter, update);

Generated at Wed Feb 07 21:41:30 UTC 2024 using Jira 9.7.1#970001-sha1:2222b88b221c4928ef0de3161136cc90c8356a66.