Insert JSON into your existing MongoDB collection

I realized my mistake :) Thanks guys. I have one more question, suppose I have several documents with the structure below in the "Client" 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 shown below:

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

Could you tell me how I can update the address array for customerId = 100 using the above JSON file in my C # code.

Please suggest.

Thanks in advance:)


I am writing C # (C sharp) (. Net) code to insert a JSON file in mongoDB. I have a jsonfile "records.JSON" that has several documents on the same line in it, for example:

[{"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 for connecting and pasting into 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);
        }

: " BSON" BSON : var bsonDoc = BsonDocument.Parse (text); : Cannot deserialize BsonDocumet from BsonType Array.

- , JSON mongoDB.??

. .

+4
2

, JSON.

, , 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);

    }

, !

.

+9

, JSON .

JSON , : http://jsonformatter.curiousconcept.com/, , , :

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

All Articles