将JSON插入现有的MongoDB集合中

时间:2015-04-08 11:20:16

标签: c# json mongodb mongodb-.net-driver bson

我理解我的错误:)谢谢你们。 我还有一个问题,假设我在"客户"中有以下结构的多个文件。集合。

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

}

现在我有一个JSON文件如下:

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

请告诉我如何在c#代码中使用上述JSON文件更新customerId = 100的地址数组。

请建议。!

提前致谢:)


我正在编写一个C#(C sharp)(。Net)代码,用于在mongoDB中插入JSON文件。我有一个jsonfile" records.JSON"它在一行中有多个文档,如:

[{"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"}]}]

我需要将此JSON文件插入现有的MongoDB集合中。 到目前为止,我有以下代码连接并插入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);
        }

但是这给了我错误:&#34;数组不能写入BSON文件的根级别&#34; 如果我将BSON解析为:var bsonDoc = BsonDocument.Parse (text); 它给了我错误:Cannot deserialize BsonDocumet from BsonType Array.

任何人都可以帮我理解如何将JSON文件插入mongoDB Collection。 ??

感谢任何帮助..提前致谢。

2 个答案:

答案 0 :(得分:13)

假设您有一个有效的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);

    }

我希望它适合你!

问候。

答案 1 :(得分:2)

正如之前的回答者所说,您的JSON格式无效。

如果你将JSON传递给像这样的验证器:http://ideone.com/I5XgXK你会发现问题与这里显示的不必要的括号有关:

[
     {
        "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"
           }
        ]
     }
  ]