简体   繁体   English

使用 C# 将 Json 文档写入 mongoDB

[英]Write Json document to mongoDB using C#

I have a CSV file which contains我有一个 CSV 文件,其中包含

Name, EmployeID
Kishore, 235

I need to read Name & EmployeID from above csv file, update name and EmployeID in below JSON and write updated Json to mongodb collection.我需要从上面的 csv 文件中读取名称和 EmployeID,在下面的 JSON 中更新名称和 EmployeID,并将更新的 Json 写入 mongodb 集合。

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

Can you please help me with any code snippet using C#你能帮我处理任何使用 C# 的代码片段吗

Appreciate your help感谢你的帮助

The simplest form to mimic the insert statement from your comment is to use a collection of type BsonDocument and insert the document like this:模仿评论中的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" }
            });

This creates the following document:这将创建以下文档:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM