【问题标题】:How to Specify field MongoDB Definition dynamically using a string in F#如何在 F# 中使用字符串动态指定字段 MongoDB 定义
【发布时间】:2021-04-15 02:49:47
【问题描述】:

我开始使用强类型定义

let coll: IMongoCollection<NZPostDataItem> = MongoUtil.getNzpostCollection()
let filter = Builders<NZPostDataItem>.Filter.And(
        Builders<NZPostDataItem>.Filter.Gte((fun n -> n.dateLodged),DateTime(2020,10,1)),
        Builders<NZPostDataItem>.Filter.Eq((fun n -> n.eshipMatched.Value) , true)
)// ... etc 

问题是,我需要动态运行查询,因为其中一个对象包含 BSONDocument

    let coll: IMongoCollection<NZPostDataItem> = MongoUtil.getNzpostCollection()
    let t = Nullable<bool> true
    let filter = 
        FilterDefinition<BsonDocument>.op_Implicit(
            "{
            eshipMatched:true, 
            'eship.order.carrier_service_code':'TDEAUS',
            dateLodged: {$gte: new Date('01-Oct-2020')}}
            )"
        )

直接在 MongoDB 上运行。但是当我尝试查询它时,我得到一个编译时错误。

let results = coll.Find(filter).Limit(10000).ToList<NZPostDataItem>()

严重性代码描述项目文件行抑制状态 错误 FS0041 方法“查找”没有重载匹配。

已知的参数类型:FilterDefinition

Available overloads:
 - (extension) IMongoCollection.Find<'TDocument>(filter: FilterDefinition<'TDocument>,?options: 
FindOptions) : IFindFluent<'TDocument,'TDocument> // Argument 'filter' doesn't match
 - (extension) IMongoCollection.Find<'TDocument>(filter: 
Linq.Expressions.Expression<Func<'TDocument,bool>>,?options: FindOptions) : 
IFindFluent<'TDocument,'TDocument> // Argument 'filter' doesn't match   

所以 - 我可以看到有什么问题 - 可能有两种类型的定义 - 但我需要动态过滤但返回一个强类型对象。

【问题讨论】:

    标签: mongodb f#


    【解决方案1】:

    正如您所说,您希望过滤器既是动态的又是强类型的,所以这就是您应该创建的:

    let filter = 
        FilterDefinition<NZPostDataItem>.op_Implicit("{ ... }")
    

    【讨论】:

      猜你喜欢
      • 2021-08-18
      • 1970-01-01
      • 2018-04-27
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多