[CSHARP-1935] How to delete chunk files Created: 08/Mar/17  Updated: 27/Oct/23  Resolved: 08/Mar/17

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

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

OS : Windows 7
.Net : 4.6.1



 Description   

In Mongocsharp driver, there was an option to delete documents in "fs.chunks" collection using gridfs object.
But, in MongoDB Driver 2.4.2, how can we delete documents from only "fs.chunks"?

Is it okay to delete documents in "fs.chunks" without using GridFS bucket object, ie, by using

Database.GetCollection<GridFSFileInfo>(fs.chunks);

Thanks in advance,
Irshad



 Comments   
Comment by Ummer Irshad [ 10/Mar/17 ]

Explanations are clear.
Thank you Robert Stam for your time and effort.

Comment by Robert Stam [ 09/Mar/17 ]

The equivalent in 2.x to your 1.11 code is:

var chunksCollection = database.GetCollection<BsonDocument>("fs.chunks");
chunksCollection.DeleteMany(filter);

If you might be using non-default bucket names you could safely get the collection like this instead:

var chunksCollection = database.GetCollection<BsonDocument>(bucket.Options.BucketName + ".chunks");

Note that if your upload failed part way whatever caused your upload to fail will almost certainly cause your attempts to clean up to fail in the same way.

It is impossible to have chunk sizes greater than 16MB because each chunk is a document in MongoDB and MongoDB limits documents to 16MB (so the chunk actually must be slightly smaller than 16MB to allow room for a small amount of overhead for element names).

You should always use GridFS methods to manipulate GridFS data when possible. It is possible to manipulate the GridFS collections directly if you wish because they are really just regular MongoDB collections, but if you do so you must exercise caution that you do not corrupt the data.

Comment by Ummer Irshad [ 09/Mar/17 ]

I am trying to handle exception cases, where I would like to delete all the chunks based on Files_Id.

In 1.11, using below code, we were able to do so.

MongoGridFS.Chunks.Remove(yourquery);

My question is: if by chance some document got inserted partially to fs.chunks collection. In this case, we would like to retry insertion of same document once again after deleting the partially inserted fs.chunks.
Whether this is possible with 2.4.2?

One more doubt: Can we delete a chunk having size more than 16 MB, using below code?
As it is a GridFS item, is there anything like one should use only GridFS bucket object for deleting anything related to GridFS?

Database.GetCollection<GridFSFileInfo>(fs.chunks);

Comment by Robert Stam [ 08/Mar/17 ]

I'm not sure what 1.x method you are referring to.

But the fs.files and fs.chunks collections are just regular collections in MongoDB and you can manipulate them directly like any other collection as long as you are careful about what you are doing and don't destroy the integrity of the GridFS data.

If what you want to do is delete an entire GridFS files (both the fs.files directory entry and the chunks in fs.chunks) use:

bucket.Delete(id);

If you manually delete a file's chunks in fs.chunks but don't delete the corresponding directory entry in fs.files you will have corrupted the GridFS bucket.

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