Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/mongodb/13.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何使用F#和MongoDB处理不存在的(可选)字段_Mongodb_F# - Fatal编程技术网

如何使用F#和MongoDB处理不存在的(可选)字段

如何使用F#和MongoDB处理不存在的(可选)字段,mongodb,f#,Mongodb,F#,我已经设法很容易地将数据序列化为类型 let coll = db.GetCollection<PostDataItem>("postItems") 我不能使用F#选项类型,因为它仍然需要一个值,但是标准的.Net驱动程序有没有一种方法可以在类型中存在字段但数据库中可能还不存在字段的情况下序列化数据。我希望它默认为null或None,但不希望必须更改数据库中的每个记录 我的类型定义如下所示: type PostDataItem = { _id: BsonO

我已经设法很容易地将数据序列化为类型

let coll = db.GetCollection<PostDataItem>("postItems")
我不能使用F#选项类型,因为它仍然需要一个值,但是标准的.Net驱动程序有没有一种方法可以在类型中存在字段但数据库中可能还不存在字段的情况下序列化数据。我希望它默认为null或None,但不希望必须更改数据库中的每个记录

我的类型定义如下所示:

type PostDataItem = {
    _id: BsonObjectId
    dateLodged: DateTime
    productCode: string
    productDescription: string
    clientReference: string
    manifestContract: string
    client: string
    quantity: string
    unitPrice: string
    gst: string
    total: string
    clientReference2: string
    weight: string
    reference1: string
    ticketNumber: string
    transactioncode: string
    invoiceExplanationField1: string
    invoiceExplanationField2: string
    invoiceExplanationField3: string
    invoiceExplanationField4: string
    invoiceExplanationField5: string
    invoiceExplanationField6: string
    orderNumberToCheck: string
    isUnique: Nullable<bool>
    expectedPrice: Nullable<decimal>
type PostDataItem={
_id:BsonObjectId
日期:日期时间
产品代码:字符串
productDescription:字符串
clientReference:string
合同名称:字符串
客户端:字符串
数量:字符串
单价:字符串
商品及服务税:字符串
总计:字符串
clientReference2:字符串
重量:细绳
参考文献1:字符串
票号:字符串
transactioncode:字符串
invoiceExplanationField1:字符串
invoiceExplanationField2:字符串
invoiceExplanationField3:字符串
invoiceExplanationField4:字符串
invoiceExplanationField5:字符串
invoiceExplanationField6:字符串
orderNumberToCheck:字符串
isUnique:可为空
预期价格:可为空
想法?

您可以使用以下属性:

open MongoDB.Bson.Serialization.Attributes

type MyType =
    {
        [<BsonDefaultValue("MyDefaultValue")>]
        MyNewField : string
    }

你能提供你的类型描述吗?@dododo谢谢-我已经用类型描述更新了描述。在你接受回答时,我正在更新我的回答。只是想确保你看到了更新的信息。@brianberns是的-我看到了-谢谢。我添加了[]还有。学习这些小问题似乎很慢。我现在被困在下一个没有更新的问题上——这里
open MongoDB.Bson.Serialization.Attributes

type MyType =
    {
        [<BsonDefaultValue("MyDefaultValue")>]
        MyNewField : string
    }
[<CLIMutable>]
type MyType =
    {
        MyNewField : string   // will automatically default to null
    }