【问题标题】:MongoDb and F#: How to get min and max values of a field in a collection?MongoDb 和 F#:如何获取集合中字段的最小值和最大值?
【发布时间】:2020-07-15 12:04:51
【问题描述】:

我正在使用 C# MongoDb 驱动程序的最新(在撰写本文时)版本 (2.8)。我在 F# 中使用它。我想获取一个字段的最小值和最大值。

关于如何使用 F# 中的 MongoDb(最新版本)似乎没有太多内容,所以如果我遗漏了什么,我深表歉意。

我唯一能做的就是以下

        let client = new MongoClient(connString)
        let db = client.GetDatabase("AirQuality")
        let col = db.GetCollection<ReadingValue>("ReadingValue")
        let toDictionary (map : Map<_, _>) : Dictionary<_, _> = Dictionary(map)

        let minb = ["$min", "$ReadingDate"] |> Map.ofList |> toDictionary
        let maxb = ["$max", "$ReadingDate"] |> Map.ofList |> toDictionary
        let grpb = 
            [
                "_id", null
                "min", minb
                "max", maxb
            ] |> Map.ofList |> toDictionary

        let aggb = ["$group", grpb] |> Map.ofList |> toDictionary
        let doc = new BsonDocument(aggb)
        let pl = new BsonDocumentPipelineStageDefinition<ReadingValue,string>(doc)
        let epl = new EmptyPipelineDefinition<ReadingValue>()
        let finalPipeline = epl.AppendStage(pl)

        use result = col.Aggregate(finalPipeline)

但它会引发运行时错误Cannot deserialize a 'String' from BsonType 'Document'.

顺便说一句,我很惊讶在 F# 中使用 MongoDb 是多么的尴尬。

发布关闭编辑:

这个问题是关于如何在 F# 中完成任务。问题linked to 迎合其他语言(可能是mongo shell)。在 F# 中无法使用这些技术(据我所知)。

【问题讨论】:

    标签: mongodb f# mongodb-.net-driver


    【解决方案1】:

    您至少可以使用dict 函数简化字典的构造:

    let grpb =
        dict [
            "_id", null
            "min", dict ["$min", "$ReadingDate"]
            "max", dict ["$max", "$ReadingDate"]
        ]
    let aggb = new BsonDocument(dict ["$group", grpb])
    

    【讨论】:

      【解决方案2】:

      我可以使用它

          type MinMax = {_id:obj; min:string; max:string}
      
          let client = new MongoClient(connString)
          let db = client.GetDatabase("AirQuality")
          let col = db.GetCollection<ReadingValue>("ReadingValue")
          let toDictionary (map : Map<_, _>) : Dictionary<_, _> = Dictionary(map)
      
          let minb = ["$min", "$ReadingDate"] |> Map.ofList |> toDictionary
          let maxb = ["$max", "$ReadingDate"] |> Map.ofList |> toDictionary
          let grpb = 
              [
                  "_id", null
                  "min", minb
                  "max", maxb
              ] |> Map.ofList |> toDictionary
      
          let aggb = ["$group", grpb] |> Map.ofList |> toDictionary |> (fun x -> new BsonDocument(x))
      
          let pl = new BsonDocumentPipelineStageDefinition<ReadingValue,MinMax>(aggb)
      
          let epl = new EmptyPipelineDefinition<ReadingValue>()
          let finalPipeline = epl.AppendStage(pl)
          use result = col.Aggregate(finalPipeline)
          let minMax = result.ToList() |> Seq.head
      

      但我认为这很难看。难道没有更简单的方法吗?

      【讨论】:

        猜你喜欢
        • 2021-11-08
        • 2019-06-24
        • 2018-05-06
        • 1970-01-01
        • 1970-01-01
        • 2015-11-11
        • 1970-01-01
        • 1970-01-01
        • 2016-06-29
        相关资源
        最近更新 更多