0
点赞
收藏
分享

微信扫一扫

c# mongodb insert 忽略Null参数入库

栖桐 2023-02-04 阅读 49


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);

举报

相关推荐

0 条评论