[CSHARP-763] MongoCommandException: Command 'filemd5' failed Created: 25/Jun/13  Updated: 20/Mar/14  Resolved: 27/Jun/13

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

Type: Bug Priority: Blocker - P1
Reporter: Majid Sameni Assignee: Robert Stam
Resolution: Done Votes: 0
Labels: driver
Remaining Estimate: Not Specified
Time Spent: Not Specified
Original Estimate: Not Specified

Backwards Compatibility: Fully Compatible

 Description   

We are getting the following exception on replica set using GridFS. I read online about manually adding the index but shouldn't the C# driver automatically do so? How can we fix this?

Exception: MongoDB.Driver.MongoCommandException: Command 'filemd5' failed: need an
index on

{ files_id : 1 , n : 1 }

(response: { "errmsg" : "need an index on

{ files_id : 1 , n : 1 }

", "ok" : 0.0 })



 Comments   
Comment by Robert Stam [ 25/Jun/13 ]

The driver uses EnsureIndex to create the indexes. Most drivers (the C# driver included) cache locally whether EnsureIndex has been called before or not, but they are unable to detect changes that happen outside of their control, as in dropping the database externally.

Because of this most drivers are changing EnsureIndex to no longer cache whether it thinks the index already exists or not. See:

https://jira.mongodb.org/browse/CSHARP-736

This change will be in the 1.9 version of the C# driver.

Your workaround for now is to recreate the indexes manually after you drop the database.

Comment by Majid Sameni [ 25/Jun/13 ]

My colleague is saying that this happens when he drops the database when connected to master. I assume that drops the indexes as well but will the next upload automatically re-crate the database create the indexes?

Exception: MongoDB.Driver.MongoCommandException: Command 'filemd5' failed: need an index on

{ files_id : 1 , n : 1 }

(response: { "errmsg" : "need an index on

{ files_id : 1 , n : 1 }

", "ok" : 0.0 })
at MongoDB.Driver.MongoDatabase.RunCommandAs(Type commandResultType, IMongoCommand command)
at MongoDB.Driver.MongoDatabase.RunCommandAs[TCommandResult](IMongoCommand command)
at MongoDB.Driver.GridFS.MongoGridFS.Upload(Stream stream, String remoteFileName, MongoGridFSCreateOptions createOptions)

This is the code that is called on the next upload into the database that was deleted earlier on

private MongoGridFS GetRiskStorageFS()

{ var mongoServer = MongoServer.Create(m_connectionString); var dbName = "RiskStorage"; var db = mongoServer.GetDatabase(dbName); var myFS = new MongoGridFS(db); return myFS; }

Could this be because something is cached in the drive as the client code does not get reset on database drop.

Comment by Robert Stam [ 25/Jun/13 ]

Drivers usually create the required indexes automatically, as long as the following conditions are true:

1. You are uploading a file (not downloading)
2. You are connected to the primary of the replica set
3. The fs.files collection does not already have more than 1000 files

Can you provide a bit more information about your use case?

A stack trace would be helpful also to determine exactly where in the driver the exception was thrown.

Nothing in the driver would ever cause this index to be removed.

If these indexes were inadvertently removed, you can recreate them using the MongoDB shell as follows:

> use <yourdatabasename>
> db.fs.files.ensureIndex({ filename : 1, uploadDate : 1})
> db.fs.chunks.ensureIndex({ files_id : 1, n : 1 })

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