简体   繁体   中英

Write Json document to mongoDB using C#

I have a CSV file which contains

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.

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

Can you please help me with any code snippet using 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:

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"
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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