Issue importing document into mongo with C#

1,050 views
Skip to first unread message

Stefan Schielke

unread,
Mar 5, 2014, 3:02:51 PM3/5/14
to mongod...@googlegroups.com

I have a large json document that I am trying to import into Mongo using C#.

My file that I create the document with currently creates the document as one long string.

As a one line string, there are 7126 characters. If I convert it to a json array it will be 387 lines.

When I try to import it as a one line document it errors out with

A first chance exception of type 'System.InvalidOperationException' occurred in MongoDB.Bson.dll
An unhandled exception of type 'System.InvalidOperationException' occurred in MongoDB.Bson.dll
Additional information: Duplicate element name 'Violation'.

I am parsing the data with:

System.IO.StreamReader file = new System.IO.StreamReader(fileName);
line = file.ReadToEnd();
var bsonDoc = BsonDocument.Parse(line);
insertDoc(bsonDoc, collection);

The 'duplicate' element name is a valid element in my document. Direct import into mongo using the command line creates no problems. If I process the document as a json array, it has not problem with the import through C#.

Is there a different way that I should be parsing the document as it comes in so that it can distinguish the objects inside?

Victor Hooi

unread,
Mar 26, 2014, 4:11:36 AM3/26/14
to mongod...@googlegroups.com, schielk...@gmail.com
Hi Stefan,

Thanks for posting.

I'm investigating your issue - if possible, do you think you could provide the following additional information:
  1. The contents of your JSON input document - you could either attach it here (in Google Groups), or if you prefer, you could post it to http://gist.github.com
  2. A complete listing of the code you're attempting to run? For example  - your snippet referred to a insertDoc() method, but the source for this was not listed.

If you don't feel comfortable posting the above publicly, please let me know, and we can work something out.

I attempted to reproduce your error with the information so far, and was not able to produce that error message.

I used the following JSON:

{ "name": "Bob Hope", "age": 21, "gender": "Male" }

And below is my C# code:

using System;
using MongoDB.Bson;
using MongoDB.Driver;
namespace ReadJSONIntoMongoDB
{
class MainClass
{
public static void Main (string[] args)
{
var connectionString = "mongodb://localhost";
var client = new MongoClient(connectionString);
var server = client.GetServer();
var database = server.GetDatabase("test");
var collection = database.GetCollection<BsonDocument>("bson_import");
string text = System.IO.File.ReadAllText(@"foo.txt");
var bsonDoc = BsonDocument.Parse (text);
collection.Insert (bsonDoc);
}
}
}

I've tested the above with the MongoDB C# Driver 1.8.3 and 1.9-rc1, and with MongoDB 2.4.9 and 2.6.0-rc2.

Cheers,
Victor

Stefan Schielke

unread,
Mar 26, 2014, 9:58:10 AM3/26/14
to mongod...@googlegroups.com, schielk...@gmail.com
Hello Victor,

Thank you for the reply.
This is for a University project, so I cannot upload the file since it has actual client data inside.

But here is the snip-it from the JSON file where the error occurs:
[ { "ContextId": "Vehicle", "ReportNumbers": { "values": [""10000000", "10000001", "10000002""] }, "RelatedTo": { "values": [] }, "DomainObjects": { "values": [] }, "Id": "z5agy1eu-zbkv-5sne-rhg6-ammteeg8sri2" },
{ "ContextId": "Violation", "ReportNumbers": { "values": ["10000000"] }, "RelatedTo": { "values": ["10000001", "10000002"] }, "DomainObjects": { "values": [] }, "Id": "z2ax1bf7-qr8z-tatb-pwf8-v3158vw5op47" },
{ "ContextId": "Violation", "ReportNumbers": { "values": ["10000001"] }, "RelatedTo": { "values": ["10000000", "10000002"] }, "DomainObjects": { "values": [] }, "Id": "7sgiaogk-4obg-yg5r-bg2r-dgekccy388f6" },
{ "ContextId": "Violation", "ReportNumbers": { "values": ["10000002"] }, "RelatedTo": { "values": ["10000000", "10000001"] }, "DomainObjects": { "values": [] }, "Id": "bgbmnnfz-63y8-k7pi-tmii-c5c68y8m7ciu" } ]

My method that inputs the file:
public static void insertNewDocFromFile(string fileName, MongoCollection<BsonDocument> collection)
{
 //read file into string and parse to BSON Document. Add document to collection
string line;
System.IO.StreamReader file = new System.IO.StreamReader(fileName);
line = file.ReadToEnd();

BsonDocument bsonDoc = new BsonDocument(true);
bsonDoc = convertStringToDoc(line);
collection.Insert(bsonDoc);
}

I will try tonight to see if the code you provided will handle the file.
Thanks for your input and assistance.

Regards,
Stefan
Reply all
Reply to author
Forward
0 new messages