C# 添加数据到MongoDB
更新时间:2022-12-20 11:56在nuget里面安装MongoDB.Driver
using MongoDB.Bson;
using MongoDB.Driver;
public void Add()
{
User[] users = new User[] {
new User(){Name="张三",Age=18},
new User(){Name="李四",Age=19},
new User(){Name="王五",Age=20}
};
//用户名 lqwvje 密码 123456
MongoClient client = new MongoClient("mongodb://lqwvje:123456@192.168.1.242:27017");
//获取database
IMongoDatabase mydb = client.GetDatabase("myDB");
//获取collection
IMongoCollection<User> collection = mydb.GetCollection<User>("user");
collection.InsertMany(users);//多条数据一起添加
//collection.InsertOne(users[0]);//添加一条数据
}