向MongoDB写一个受歧视的联盟可能吗?
运行以下代码:向MongoDB写一个受歧视的联盟可能吗?,mongodb,f#,Mongodb,F#,运行以下代码: type Test = | L of int | M of string | N of bool let client = new MongoClient("mongodb://localhost:27017") let _db = client.GetDatabase("TestDB") let collection = _db.GetCollection<Test>("TestLog") collection.InsertOne(L 22) 所
type Test =
| L of int
| M of string
| N of bool
let client = new MongoClient("mongodb://localhost:27017")
let _db = client.GetDatabase("TestDB")
let collection = _db.GetCollection<Test>("TestLog")
collection.InsertOne(L 22)
所以看起来Mongo的.NET驱动程序不知道如何处理受歧视的工会。我已经看到了一些库,但它们非常古老,我想先检查一下框架中是否没有内置任何内容。我尝试的另一件事是使用Newtonsoft的JSON序列化程序,如下所示:
let Json = JsonConvert.SerializeObject(L 22)
let doc = BsonSerializer.Deserialize<BsonDocument>(Json)
let Json=JsonConvert.SerializeObject(L 22)
让doc=BsonSerializer.Deserialize(Json)
它将所有内容都放入数据库,但它们都是字符串类型,而不是它们应该是的类型(在本例中为int)。序列化库非常有效地处理了这一问题。它可以很好地处理(并保留类型)有区别的联合、记录类型、记录类型中嵌入的有区别的联合等。也许您应该看看Chiron()。它也是一个专门针对F#的JSON序列化程序。如果你能汇报一下你在这两个和mongodb中的任何一个的经历,那就太好了。
let Json = JsonConvert.SerializeObject(L 22)
let doc = BsonSerializer.Deserialize<BsonDocument>(Json)