InvalidOperationException: Duplicate element name 'filename'.

808 views
Skip to first unread message

Nathanael Jones

unread,
Feb 21, 2012, 1:29:34 PM2/21/12
to mongodb-user
I'm completely new to MongoDB, so perhaps this is something obvious.
But extensive googling didn't turn up anything.

I set up a brand new MongoDB account, created a database named
'resizer-test' with accnt 'resizer' and password 'test'. I then
uploaded an image named 'flames.jpg' using the web interface
successfully.

I then tried to download the file from C# using the following code and
the official C# driver (version 1.3.1)

string connectionString = "mongodb://resizer:te...@staff.mongohq.com:
10028/resizer-test";
this.db = MongoDatabase.Create(connectionString);
gridSettings = new MongoGridFSSettings();
grid = db.GetGridFS(gridSettings);
return grid.OpenRead("flames.jpg");


But immediately got this exception:

InvalidOperationException: Duplicate element name 'filename'.

at MongoDB.Bson.BsonDocument.Add(BsonElement element) in C:\work\10gen
\mongodb\mongo-csharp-driver\Bson\ObjectModel\BsonDocument.cs:line 452
at MongoDB.Bson.BsonDocument.Deserialize(BsonReader bsonReader,
Type nominalType, IBsonSerializationOptions options) in C:\work\10gen
\mongodb\mongo-csharp-driver\Bson\ObjectModel\BsonDocument.cs:line 720
at MongoDB.Bson.BsonDocument.ReadFrom(BsonReader bsonReader) in C:
\work\10gen\mongodb\mongo-csharp-driver\Bson\ObjectModel
\BsonDocument.cs:line 393
at MongoDB.Bson.Serialization.BsonSerializer.Deserialize(BsonReader
bsonReader, Type nominalType, IBsonSerializationOptions options) in C:
\work\10gen\mongodb\mongo-csharp-driver\Bson\Serialization
\BsonSerializer.cs:line 214
at MongoDB.Driver.Internal.MongoReplyMessage`1.ReadFrom(BsonBuffer
buffer, IBsonSerializationOptions serializationOptions) in C:\work
\10gen\mongodb\mongo-csharp-driver\Driver\Internal
\MongoReplyMessage.cs:line 94
at MongoDB.Driver.Internal.MongoConnection.ReceiveMessage[TDocument]
(BsonBinaryReaderSettings readerSettings, IBsonSerializationOptions
serializationOptions) in C:\work\10gen\mongodb\mongo-csharp-driver
\Driver\Internal\MongoConnection.cs:line 383
at MongoDB.Driver.MongoCursorEnumerator`1.GetReply(MongoConnection
connection, MongoRequestMessage message) in C:\work\10gen\mongodb
\mongo-csharp-driver\Driver\Core\MongoCursorEnumerator.cs:line 263
at MongoDB.Driver.MongoCursorEnumerator`1.GetFirst() in C:\work
\10gen\mongodb\mongo-csharp-driver\Driver\Core
\MongoCursorEnumerator.cs:line 223
at MongoDB.Driver.MongoCursorEnumerator`1.MoveNext() in C:\work
\10gen\mongodb\mongo-csharp-driver\Driver\Core
\MongoCursorEnumerator.cs:line 126
at System.Linq.Enumerable.FirstOrDefault[TSource](IEnumerable`1
source)
at MongoDB.Driver.GridFS.MongoGridFSFileInfo.Refresh() in C:\work
\10gen\mongodb\mongo-csharp-driver\Driver\GridFS
\MongoGridFSFileInfo.cs:line 447
at MongoDB.Driver.GridFS.MongoGridFSFileInfo.get_Exists() in C:\work
\10gen\mongodb\mongo-csharp-driver\Driver\GridFS
\MongoGridFSFileInfo.cs:line 150
at
MongoDB.Driver.GridFS.MongoGridFSStream..ctor(MongoGridFSFileInfo
fileInfo, FileMode mode, FileAccess access) in C:\work\10gen\mongodb
\mongo-csharp-driver\Driver\GridFS\MongoGridFSStream.cs:line 76
at MongoDB.Driver.GridFS.MongoGridFSFileInfo.Open(FileMode mode,
FileAccess access) in C:\work\10gen\mongodb\mongo-csharp-driver\Driver
\GridFS\MongoGridFSFileInfo.cs:line 408
at MongoDB.Driver.GridFS.MongoGridFSFileInfo.OpenRead() in C:\work
\10gen\mongodb\mongo-csharp-driver\Driver\GridFS
\MongoGridFSFileInfo.cs:line 416
at MongoDB.Driver.GridFS.MongoGridFS.OpenRead(String
remoteFileName) in C:\work\10gen\mongodb\mongo-csharp-driver\Driver
\GridFS\MongoGridFS.cs:line 696
at
ImageResizer.Plugins.MongoReader.MongoReaderPlugin.MongoVirtualFile.Open()
in C:\Users\Administrator\Documents\resizer\Plugins\MongoReader
\MongoReaderPlugin.cs:line 79
at

http://stackoverflow.com/questions/9382347/mongodb-gridfs-error-duplicate-element-name-filename

Any ideas?

Scott Hernandez

unread,
Feb 21, 2012, 3:24:24 PM2/21/12
to mongod...@googlegroups.com
Did you already have a file with that name in it -- it looks like it?
If so, how did it get there -- from another langauge/system? Can you
dump that document from the gridfs files collection and post the
results?

> --
> 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.
>

Nathanael D. Jones

unread,
Feb 21, 2012, 4:06:19 PM2/21/12
to mongod...@googlegroups.com
According the the MongoHQ web interface, I only have 1 file:

  ID Filename    
1 4f43d19b68cd87000800000c flames.jpg Attributes View File

File Details
Key Value
_id : "4f43d19b68cd87000800000c" ,
filename : "flames.jpg" ,
contentType : "image/jpeg" ,
length : 2926346 ,
chunkSize : 262144 ,
uploadDate : "2012-02-21T17:17:16Z" ,
md5 : "2ffeec76bc0849be1f8dbd1f76fc2674"
The database is brand new, I created it through the web interface and manually uploaded 1 file. 

Does this help?

Robert Stam

unread,
Feb 21, 2012, 4:26:30 PM2/21/12
to mongod...@googlegroups.com
The server is returning the "filename" element twice in response to this low level query:

> db["resizer-test"].fs.files.find({filename:"flames.jpg"}).sort({uploadDate:-1})

I wonder if there is a bug in the server? Do you know what version of the server is running?

Robert Stam

unread,
Feb 21, 2012, 4:32:39 PM2/21/12
to mongod...@googlegroups.com
Could also be a bug in the code that originally uploaded the file, which might have sent an invalid document with duplicate "filename" values to the fs.files collection in the first place, so the server is just returning what was put there.

You did say you didn't use the C# driver for the file upload, right?

Nathanael D. Jones

unread,
Feb 21, 2012, 4:32:35 PM2/21/12
to mongod...@googlegroups.com
No idea - I created a new account and new database today, so whatever mongohq is running. I didn't see a version number on the web interface or find the console command to request it. Is there a Gem for managing remote instances?

Nathanael D. Jones

unread,
Feb 21, 2012, 4:33:33 PM2/21/12
to mongod...@googlegroups.com
No, I only used the web interface - the account I'm using is read only as well, although changing that didn't make a difference either.

Nathanael D. Jones

unread,
Feb 21, 2012, 4:43:57 PM2/21/12
to mongod...@googlegroups.com
I just created another database ('resizer2'), uploaded another file using the web interface, and had the exact same problem. It seems easily reproducible. 

Robert Stam

unread,
Feb 21, 2012, 4:57:47 PM2/21/12
to mongod...@googlegroups.com
Yes, I've reproduced it and confirmed that the server at MongoHQ is returning the "filename" element twice.

What I can't figure out is how the duplicate "filename" got there.

There is either a bug in the web interface that uploaded the file (don't know whose web interface that is, is it something MongoHQ provides?) or in the server (but not likely in the server because I can't find any JIRAs for it).

Could you try uploading the GridFS file using the C# driver instead of the web interface? That would help isolate the problem.

Robert Stam

unread,
Feb 21, 2012, 5:35:10 PM2/21/12
to mongod...@googlegroups.com
I've created a test account at MongoHQ and uploaded a file using their web interface. When I then connect to that database with the mongo shell and look at the fs.files collection this is what I see:

> db.fs.files.find()
{ "_id" : ObjectId("4f4415f068cd870d41000003"), "filename" : "TextFile1.txt", "c
ontentType" : "text/plain", "length" : 6, "chunkSize" : 262144, "uploadDate" : I
SODate("2012-02-21T22:08:48.336Z"), "md5" : "ad2d611e4b060351732574b64aeaeed7",
"filename" : "TextFile1.txt" }
>

Notice that the "filename" element is there twice.

Can you bring this up with MongoHQ and ask them to check why their web interface is creating the "filename" element twice?

Nathanael D. Jones

unread,
Feb 21, 2012, 5:52:16 PM2/21/12
to mongod...@googlegroups.com
I agree - It's definitely in the MongoHQ upload interface. Using C#, I was able to upload and download images perfectly fine. 

I'm releasing a plugin for the http://imageresizing.net project that supports MongoDB - it allows this kind of URL API:


The nuget package should be published relatively soon, if anyone is interested.

Best regards,
Nathanael Jones

Robert Stam

unread,
Feb 21, 2012, 5:54:05 PM2/21/12
to mongod...@googlegroups.com
Glad to hear that both directions are working for you. Thanks for the update.

Chris Winslett

unread,
Feb 22, 2012, 9:59:45 AM2/22/12
to mongodb-user
Issued a pull request for the Ruby Mongo driver:

https://github.com/mongodb/mongo-ruby-driver/pull/91

Chris
MongoHQ

On Feb 21, 4:54 pm, Robert Stam <rob...@10gen.com> wrote:
> Glad to hear that both directions are working for you. Thanks for the
> update.
>
> On Tue, Feb 21, 2012 at 4:52 PM, Nathanael D. Jones <
>
>
>
>
>
>
>
> nathanael.jo...@gmail.com> wrote:
> > I agree - It's definitely in the MongoHQ upload interface. Using C#, I was
> > able to upload and download images perfectly fine.
>
> > I'm releasing a plugin for thehttp://imageresizing.netproject that
> > supports MongoDB - it allows this kind of URL API:
>
> > *http://localhost:50887/gridfs/id/4f44195642f73910f056eb33.jpg?width=200*
> > *http://localhost:50887/gridfs/flames.jpg?width=200&format=png&rotate=30*
> >>>>>>    ID Filename     1 4f43d19b68cd87000800000c<https://mongohq.com/databases/resizer-test/gridstores/4f43d19b68cd870...>flames.jpg
> >>>>>> Attributes<https://mongohq.com/databases/resizer-test/gridstores/4f43d19b68cd870...> View
> >>>>>> File<https://mongohq.com/databases/resizer-test/gridstores/4f43d19b68cd870...>
> >>>>>>> resizer:t...@staff.mongohq.com:
> >>>>>>>http://stackoverflow.com/questions/9382347/mongodb-gridfs-error-dupli...
> ...
>
> read more »
Reply all
Reply to author
Forward
0 new messages