Search code examples
c#mongodbmongodb-.net-drivergridfsgridfs-stream

WriteConcernException when closing Mongo GridFS file stream


I'm using MongoDB driver in C#, and store video files using GridFS.
I store the files continuously like recording a stream and once in a while (might be in middle of writing process) I ask to "play" a file.
In some cases, at that part of playing the file there's a problem:
1. the relevant GridFS file stream is opened.
2. data read ok.
3. GridFS file stream is closed -> throws WriteConcernException with the following details:

WriteConcern detected an error 'E11000 duplicate key error index:
ElSightRecs.fs.chunks.$files_id_1_n_1  dup key: { :
ObjectId('536243c4c449203a24a8f3b2'), : 2656 }'. 
(Response was { "err" : "E11000 duplicate key error index: 
ElSightRecs.fs.chunks.$files_id_1_n_1  dup key: { : 
ObjectId('536243c4c449203a24a8f3b2'), : 2656 }", "code" : 11000, "n" : 0,
"connectionId" : 1111, "ok" : 1.0 }).

`

why do i get any write error when all I'm doing is closing the file stream?


Solution

  • well, I'm not sure if it's a bug in MongoDB driver or not, but I've found the problem.
    I opened the GridFS file with OpenRead() so I thought I'm well protected. apparently not.
    I had a bug that called Seek to a position higher than the file length. this caused the closing problem...
    don't know why, don't know who ... but maybe other could use this tip :)