将JSON插入现有的MongoDB集合中

dar*_*rsh 6 c# json mongodb bson mongodb-.net-driver

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

{
    "customerId":100,
    "FirstName":"xyz",
    "lastname":"pqr",
    "address":[
       {
          "house":44,
          "city":"Delhi",
          "country":"india"
       }
    ],
    "employer":[
       {
          "cmpName":"ABC",
          "type":"IT"
       }
    ]
Run Code Online (Sandbox Code Playgroud)

}

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

{
    "customerId":100,
    "address":[
       {
          "house":99,
          "city":"MUMBAI",
          "country":"INDIA"
       }
    ]
 }
Run Code Online (Sandbox Code Playgroud)

您能否告诉我如何使用我的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"}]}]
Run Code Online (Sandbox Code Playgroud)

我需要将此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);
        }
Run Code Online (Sandbox Code Playgroud)

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

任何人都可以帮我理解如何将JSON文件插入mongoDB集合.??

任何帮助表示赞赏..在此先感谢.

Raf*_*oni 15

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

    }
Run Code Online (Sandbox Code Playgroud)

我希望这个对你有用!

问候.

  • 注意:如果文本包含记录列表,则反序列化类型应该是 IEnumerable,即 BsonSerializer.Deserialize&lt;IList&lt;BsonDocument&gt;&gt; (2认同)

Ahm*_*que 3

正如前面的回答者所建议的,您的 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"
           }
        ]
     }
  ]
Run Code Online (Sandbox Code Playgroud)