using MongoDB.Bson;
using MongoDB.Bson.Serialization.Conventions;
using MongoDB.Driver;
string age = null;
var document = new
{
name = "添加到mongodb",
age = age,
};
MongoClientSettings settings= MongoClientSettings.FromConnectionString("mongodb://账号:密码@127.0.0.1:27017/");
MongoClient client=new MongoClient(settings);
ConventionRegistry.Register("IgnoreIfDefault", new ConventionPack { new IgnoreIfDefaultConvention(true) }, t => true);
var bsonDocument = document.ToBsonDocument();
InsertOneOptions options = new InsertOneOptions();
client.GetDatabase("test").GetCollection<object>("user").InsertOne(document, options);