[CSHARP-1928] What is the alternative for 'database.GridFS.SetMetadata' in MongoDB.Driver 2.4.2? Created: 28/Feb/17  Updated: 16/Nov/21  Resolved: 23/Mar/17

Status: Closed
Project: C# Driver
Component/s: Documentation, GridFS
Affects Version/s: 2.4.2
Fix Version/s: None

Type: Task Priority: Blocker - P1
Reporter: Ummer Irshad Assignee: Robert Stam
Resolution: Done Votes: 0
Labels: driver, question
Remaining Estimate: Not Specified
Time Spent: Not Specified
Original Estimate: Not Specified
Environment:

OS : Win 7
.Net Framework 4.6.1



 Description   

I want to update around 22 fields of my metadata without modifying the document inside GridFS collection.
In Mongocsharp driver 1.11.0, I was using 'database.GridFS.SetMetadata' for the same.
Could you please share an example or documentation for the same?

Thanks in advance.



 Comments   
Comment by Robert Stam [ 23/Mar/17 ]

In the 2.x driver API you would use the new GridFS API to get the metadata:

ObjectId fileId = ...;
var fileInfoFilter = Builders<GridFSFileInfo<ObjectId>>.Filter.Eq(f => f.Id, fileId);
var fileInfo = bucket.Find(fileInfoFilter ).SingleOrDefault(); // could return null if file is not found
var metadata = fileInfo.Metadata; // metadata is a BsonDocument

After you have modified the metadata BsonDocument in your code you can update it in the database using the CRUD API:

var filesCollection = database.GetCollection<BsonDocument>(bucket.Options.BucketName + ".files");
var filesCollectionFilter = Builders<BsonDocument>.Filter.Eq("_id", fileId);
var update = Builders<BsonDocument>.Update.Set("metadata", metadata);
filesCollection.UpdateOne(filesCollectionFilter, update);

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