簡體   English   中英

使用 C# 將 Json 文檔寫入 mongoDB

[英]Write Json document to mongoDB using C#

我有一個 CSV 文件,其中包含

Name, EmployeID
Kishore, 235

我需要從上面的 csv 文件中讀取名稱和 EmployeID,在下面的 JSON 中更新名稱和 EmployeID,並將更新的 Json 寫入 mongodb 集合。

{    
    "name": kishore,
    "EmployeID": "235",
    "Gender": "Male",
}

你能幫我處理任何使用 C# 的代碼片段嗎

感謝你的幫助

模仿評論中的insert語句的最簡單形式是使用BsonDocument類型的集合並像這樣插入文檔:

var client = new MongoClient("MONGODB_CONNSTR");
var db = client.GetDatabase("MONGODB_DB_NAME");
var employees = db.GetCollection<BsonDocument>("employees");
await employees.InsertOneAsync(new BsonDocument
            {
                { "EmployeeId", 235 },
                { "Name", "kishore" },
                { "Gender", "Male" }
            });

這將創建以下文檔:

{  
  "_id": {    
    "$oid": "62d7b6d25c248b9684eee64d"  
  },  
  "EmployeeId": 235,
  "Name": "kishore",
  "Gender": "Male"
}

暫無
暫無

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

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