gpt4 book ai didi

mongodb - f# 使用记录在 MongoDB 上插入

转载 作者:可可西里 更新时间:2023-11-01 10:02:14 26 4
gpt4 key购买 nike

我一直在尝试只使用记录插入 MongoDB,但没有成功。

我的问题是我想创建一个简单的插入函数,我发送一个泛型并将它插入到数据库中。

像这样。

let insert (value: 'a) = 
let collection = MongoClient().GetDatabase("db").GetCollection<'a> "col"
collection.InsertOne value

从这个函数中,我尝试插入以下记录。

// Error that it can't set the Id
type t1 = {
Id: ObjectId
Text: string
}
// Creates the record perfectly but doesn't generate a new Id
type t2 = {
Id: string
Text: string
}
// Creates the record and autogenerates the Id but doesn't insert the Text, and there are two Ids (_id, Id@)
type t3 = {
mutable Id: ObjectId
Text: string
}
// Creates the record and autogenerates the Id but for every property it generates two on MongoDB (_id, Id@, Text, Text@)
type t4 = {
mutable Id: ObjectId
mutable Text: string
}

那么有没有人能为此想到解决方案,或者我是否不得不使用类。

// Works!!!
type t5() =
member val Id = ObjectId.Empty with get, set
member val Name = "" with get, set

另外,有没有人知道为什么当 C# MongoDB 库翻译可变变量时,他会在末尾获得带有 @ 的属性?我愿意将我的所有属性设置为可变的,尽管这不是我的第一选择,让他在数据库上创建多个属性是非常糟糕的。

最佳答案

您可以尝试使用 CLIMutable 注释您的记录(并且没有可变字段)。

@ 最终出现在数据库中,因为 MongoDB 使用反射和 F# 实现带有支持字段 fieldName@mutable

关于mongodb - f# 使用记录在 MongoDB 上插入,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47337135/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com