簡體   English   中英

將JSON插入現有的MongoDB集合中

[英]Insert JSON into an existing MongoDB collection

我理解我的錯誤:)謝謝你們。 我還有一個問題,假設我在“客戶”集合中有多個具有以下結構的文檔。

{
    "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);
        }

但是這給了我錯誤:“數組不能寫入BSON文檔的根級別”並且如果我將BSON解析為: var bsonDoc = BsonDocument.Parse (text); 它給出了我的錯誤: Cannot deserialize BsonDocumet from BsonType Array.

任何人都可以幫我理解如何將JSON文件插入mongoDB集合。 ??

任何幫助表示贊賞..在此先感謝。

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

    }

我希望這個對你有用!

問候。

正如之前的回答者所建議的那樣,您的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"
           }
        ]
     }
  ]

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM