C# Driver Issue with GridFS while figuring MD5

585 views
Skip to first unread message

Patrick

unread,
Jul 7, 2011, 9:18:26 AM7/7/11
to mongodb-user
I'm using the 64 bit version of MongoDB. When trying to save a file
to GridFS with the official 10gen C# driver I keep getting:

"Command 'filemd5' failed: exception: best guess plan requested, but
scan and order required: query: { files_id:
ObjectId('4e15b0c5905ad41e0c4a10b4') } order: { files_id: 1, n: 1 }
choices: { $natural: 1 } (response: { \"errmsg\" : \"exception: best
guess plan requested, but scan and order required: query: { files_id:
ObjectId('4e15b0c5905ad41e0c4a10b4') } order: { files_id: 1, n: 1 }
choices: { $natural: 1 } \", \"code\" : 13284, \"ok\" : 0.0 })"

It seems like the md5 Mongo is calculating doesn't match what the
driver is calculating. If I run the driver code and skip the md5
check, it will store the file.

Any ideas? I've reindexed, deleted db and recreated to no avail. Is
it something with the 64 bit version?

Robert Stam

unread,
Jul 7, 2011, 9:28:40 AM7/7/11
to mongodb-user
This error typically occurs when the fs.chunks collection is missing
an index on { files_id : 1, n : 1 }.

The C# driver normally creates this index automatically, but only if
the fs.files collection has less than 1000 files uploaded already.

Can you run this in the mongo shell to confirm which indexes exist:

> use <database> # replace <database> with the name of the database holding your GridFS files

> db.fs.files.getIndexes()

> db.fs.chunks.getIndexes()

and provide the output?

Can you also provide a stack trace so I can see what GridFS method you
called and what other methods were called before the exception was
thrown?

Nat

unread,
Jul 7, 2011, 9:31:57 AM7/7/11
to mongod...@googlegroups.com
You just need to create an index on { files_id : 1, n: 1} on gridfs collection 

db.fs.chunks.ensureIndex({files_id:1, n:1}, {unique: true});

Patrick

unread,
Jul 7, 2011, 9:33:33 AM7/7/11
to mongodb-user
Actually it is failing on this statement in the Upload method:

var md5Result = database.RunCommand(md5Command);

md5Command = { "filemd5" : ObjectId("4e15b48e905ad41e0c4a10ba"),
"root" : "fs" }

It never gets a chance to compare the MD5's

Patrick

unread,
Jul 7, 2011, 9:35:50 AM7/7/11
to mongodb-user
I'll give it a whirl, I'm a neophyte in this... I wonder why it's not
getting created?

Robert Stam

unread,
Jul 7, 2011, 9:37:43 AM7/7/11
to mongodb-user
The filemd5 command runs on the server and requires the existence of
the { files_id : 1, n : 1 } index.

It would still be useful to run the mongo shell commands from my
previous post to confirm the existence (or not) of this index.

Since the driver should have created this index automatically, I would
also like to see a stack trace showing which GridFS method you called
and what other methods were called internally in the driver before the
exception was thrown.

On Jul 7, 9:33 am, Patrick <prlaffe...@gmail.com> wrote:

Patrick

unread,
Jul 7, 2011, 9:56:41 AM7/7/11
to mongodb-user
Does this look correct?

MongoDB shell version: 1.8.1
connecting to: test
rs:PRIMARY> db.fs.chunks.getIndexes()
[
{
"name" : "_id_",
"ns" : "test.fs.chunks",
"key" : {
"_id" : 1
},
"v" : 0
},
{
"_id" : ObjectId("4e15b6a36bc1a5b9c1b845c4"),
"ns" : "test.fs.chunks",
"key" : {
"files_id" : 1,
"n" : 1
},
"name" : "files_id_1_n_1",
"unique" : true,
"v" : 0
}
]
rs:PRIMARY>

I'm still getting the error....I am replicating if that makes any
difference.

Digging deeper into the driver it is here:

public virtual CommandResult RunCommandAs(
Type commandResultType,
IMongoCommand command
) {
var response = CommandCollection.FindOne(command); <--
*** THIS FAILS ***
if (response == null) {
var commandName =
command.ToBsonDocument().GetElement(0).Name;
var message = string.Format("Command '{0}' failed. No
response returned.", commandName);
throw new MongoCommandException(message);
}
var commandResult = (CommandResult)
Activator.CreateInstance(commandResultType); // constructor can't have
arguments
commandResult.Initialize(command, response); // so two
phase construction required
if (!commandResult.Ok) {
if (commandResult.ErrorMessage == "not master") {
server.Disconnect();
}
throw new MongoCommandException(commandResult);
}
return commandResult;
}

The response is:

response = { "errmsg" : "exception: best guess plan requested, but
scan and order required: query: { files_id:
ObjectId('4e15ba69905ad41e0c4a10c9') } order: { files_id: 1, n: 1 }
choices: { $natural: 1 } ", "code" : 13284, "ok" : 0.0 }

The stack looks like:

> MongoDB.Driver.dll!MongoDB.Driver.MongoDatabase.RunCommandAs(System.Type commandResultType = {Name = "CommandResult" FullName = "MongoDB.Driver.CommandResult"}, MongoDB.Driver.IMongoCommand command = {MongoDB.Driver.CommandDocument}) Line 826 C#
MongoDB.Driver.dll!
MongoDB.Driver.MongoDatabase.RunCommandAs<MongoDB.Driver.CommandResult>(MongoDB.Driver.IMongoCommand
command = {MongoDB.Driver.CommandDocument}) Line 801 + 0x47 bytes C#
MongoDB.Driver.dll!
MongoDB.Driver.MongoDatabase.RunCommand(MongoDB.Driver.IMongoCommand
command = {MongoDB.Driver.CommandDocument}) Line 779 + 0x2e bytes C#
MongoDB.Driver.dll!
MongoDB.Driver.GridFS.MongoGridFS.Upload(System.IO.Stream stream =
{System.IO.FileStream}, string remoteFileName = "h:\\images\\abbie -
Copy (9).jpg", MongoDB.Driver.GridFS.MongoGridFSCreateOptions
createOptions = {MongoDB.Driver.GridFS.MongoGridFSCreateOptions}) Line
855 + 0x2d bytes C#
MongoDB.Driver.dll!
MongoDB.Driver.GridFS.MongoGridFS.Upload(System.IO.Stream stream =
{System.IO.FileStream}, string remoteFileName = "h:\\images\\abbie -
Copy (9).jpg") Line 784 + 0x22 bytes C#
MongoViewer.exe!MongoViewer.Form1.storeFile(string fileName = "h:\
\images\\abbie - Copy (9).jpg") Line 65 + 0x3a bytes C#
MongoViewer.exe!MongoViewer.Form1.OnChanged(object source =
{System.IO.FileSystemWatcher}, System.IO.FileSystemEventArgs e =
{System.IO.FileSystemEventArgs}) Line 366 + 0x57 bytes C#
System.dll!System.IO.FileSystemWatcher.CompletionStatusChanged(uint
errorCode, uint numBytes, System.Threading.NativeOverlapped*
overlappedPointer = 0x0000000002646300) + 0x2e2 bytes
mscorlib.dll!
System.Threading._IOCompletionCallback.PerformIOCompletionCallback(uint
errorCode = 0, uint numBytes = 52, System.Threading.NativeOverlapped*
pOVERLAP = 0x0000000002646300) + 0x96 bytes
[Native to Managed Transition]

Robert Stam

unread,
Jul 7, 2011, 10:31:21 AM7/7/11
to mongodb-user
The indexes on fs.chunks look OK.

I know these are silly questions, but...

1. Are you sure the C# code is connecting to the same server and
database as the mongo shell?
2. Are you using slaveOk=true?

Also, we can try running the md5 command from the mongo shell and see
if it works there:

> db.runCommand({filemd5:ObjectId("4e15ba69905ad41e0c4a10c9"), root:"fs"})

Note: I took the ObjectId from your error message so presumably it
exists in your database.

I don't think the issue is related to replication, but it could be.

Patrick

unread,
Jul 7, 2011, 11:40:45 AM7/7/11
to mongodb-user
Robert-

I was using "slaveOK=true" once I took that out it worked.

What exactly was that doing to cause that error response?

Nat

unread,
Jul 7, 2011, 11:46:18 AM7/7/11
to mongod...@googlegroups.com
It looks like for some reasons your slave is missing that index. Can you connect to the slave directly via shell and create that index manually?
--
You received this message because you are subscribed to the Google Groups "mongodb-user" group.
To post to this group, send email to mongod...@googlegroups.com.
To unsubscribe from this group, send email to mongodb-user...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/mongodb-user?hl=en.

Robert Stam

unread,
Jul 7, 2011, 12:16:43 PM7/7/11
to mongodb-user
Not sure why slaveOk=true would cause this error. The command should
have been sent to the primary anyway (slaveOk is supposed to be
ignored for commands).

Exactly which version of the C# driver are you using? Perhaps this is
an issue with an older version... or at least I should know which
version you are using before attempting to reproduce.
Reply all
Reply to author
Forward
0 new messages