Insert JSON into an existing MongoDB collection

darsh picture darsh · Apr 8, 2015 · Viewed 20.7k times · Source

I understood my mistake :) Thanks guys. I have one more Question, suppose i have multiple documents with the below structure in "Customer" collection.

{
    "customerId":100,
    "FirstName":"xyz",
    "lastname":"pqr",
    "address":[
       {
          "house":44,
          "city":"Delhi",
          "country":"india"
       }
    ],
    "employer":[
       {
          "cmpName":"ABC",
          "type":"IT"
       }
    ]

}

Now i have a JSON file as below:

{
    "customerId":100,
    "address":[
       {
          "house":99,
          "city":"MUMBAI",
          "country":"INDIA"
       }
    ]
 }

Can you please tell me how can i update the address array for customerId = 100 using the above JSON file in my c# code.

Please suggest.!

Thanks in advance :)


I am writing a C# (C sharp)(.Net) code to insert a JSON file in mongoDB. i have a jsonfile " records.JSON " which has multiple document in one single row in it, like :

[{"customerId" : 100,"FirstName" : "xyz","lastname" : "pqr","address":[{"house": 44,"city" : "Delhi", "country" : "india"}],"employer":[{"cmpName" : "ABC","type" : "IT"}]}][{"customerId" : 101,"FirstName" : "LMN","lastname" : "GHI","address":[{"house": 90,"city" : "NewYork", "country" : "US"}],"employer":[{"cmpName" : "ABC","type" : "IT"}]}]

I need to insert this JSON file into an existing MongoDB collection. So far I have the following code to connect and insert to mongodb :

public static void Main (string[] args)
        {
            var connectionString = "mongodb://localhost";    
            var client = new MongoClient(connectionString);
            var server = client.GetServer();
            server.Connect();
            var database = server.GetDatabase("test");
 var collection = database.GetCollection<BsonDocument>("test_collection");
            string text = System.IO.File.ReadAllText(@"records.JSON");
            var bsonDoc = BsonArray.Parse (text);
            collection.Insert (bsonDoc);
        }

But this is giving me error as : " an array cannot be written to root level of BSON document" And if i parse BSON as : var bsonDoc = BsonDocument.Parse (text); it gives me error as : Cannot deserialize BsonDocumet from BsonType Array.

Can anybody Please help me to understand How do i insert the JSON file into the mongoDB Collection. ??

Any help is appreciated.. Thanks in advance.

Answer

Rafael Delboni picture Rafael Delboni · Aug 1, 2015

Supposing that you are have a valid JSON file.

This is what you need to do, using the new MongoDB.Driver 2.0:

public static void Main (string[] args)
    {
        var connectionString = "mongodb://localhost";

        var client = new MongoClient(connectionString);
        var database = client.GetDatabase("test");  

        string text = System.IO.File.ReadAllText(@"records.JSON");

        var document = BsonSerializer.Deserialize<BsonDocument>(text);
        var collection = database.GetCollection<BsonDocument>("test_collection");
        await collection.InsertOneAsync(document);

    }

I hope it works for you!

Regards.