如何使用 F# 过滤 MongoDB 中的正则表达式
How to Filter Regular expression in MongoDB with F#
我已经尝试了很多不同的方法来做到这一点,最终我想同时使用多个查找过滤器。
所以 - 例如,一种方式就像 https://www.codeproject.com/Articles/1228421/Dynamic-high-performance-Query-builder-for-MongoDB
尝试在 F# 中构建以下 C# 代码(并转换为我的代码)
Builders<CustomerGrid>.Filter.Regex(u => u.company,
new BsonRegularExpression(companyColumnText + "*", "I"))
我试过了
let filter1 = Builders<PostDataItem>.Filter.Regex(fun n -> n.reference1,new BsonRegularExpression("1002\d{5}"))
和(字段定义用括号括起来)
let filter1 = Builders<PostDataItem>.Filter.Regex((fun n -> n.reference1),new BsonRegularExpression("1002\d{5}"))
另一种看待它的方式是分解参数
let fd: FieldDefinition<PostDataItem> = // ?? field = reference1
let re: BsonRegularExpression = new BsonRegularExpression("1002\d{5}")
let filter1 = Builders<PostDataItem>.Filter.Regex(fd,re)
看来,如果我能弄清楚如何构建字段定义,那么它可能会起作用。
你很接近。我看到的唯一问题是 the LINQ expression has to have a return type of Object
。这在 C# 中隐式发生,但在 F# 中您必须显式转换它:
let filter1 =
Builders<PostDataItem>
.Filter
.Regex(
(fun n -> n.reference1 :> obj),
BsonRegularExpression("1002\d{5}"))
(恕我直言,这只是他们 API 中的一点 C# 偏见。希望他们有一天会修复它。)
我已经尝试了很多不同的方法来做到这一点,最终我想同时使用多个查找过滤器。
所以 - 例如,一种方式就像 https://www.codeproject.com/Articles/1228421/Dynamic-high-performance-Query-builder-for-MongoDB
尝试在 F# 中构建以下 C# 代码(并转换为我的代码)
Builders<CustomerGrid>.Filter.Regex(u => u.company,
new BsonRegularExpression(companyColumnText + "*", "I"))
我试过了
let filter1 = Builders<PostDataItem>.Filter.Regex(fun n -> n.reference1,new BsonRegularExpression("1002\d{5}"))
和(字段定义用括号括起来)
let filter1 = Builders<PostDataItem>.Filter.Regex((fun n -> n.reference1),new BsonRegularExpression("1002\d{5}"))
另一种看待它的方式是分解参数
let fd: FieldDefinition<PostDataItem> = // ?? field = reference1
let re: BsonRegularExpression = new BsonRegularExpression("1002\d{5}")
let filter1 = Builders<PostDataItem>.Filter.Regex(fd,re)
看来,如果我能弄清楚如何构建字段定义,那么它可能会起作用。
你很接近。我看到的唯一问题是 the LINQ expression has to have a return type of Object
。这在 C# 中隐式发生,但在 F# 中您必须显式转换它:
let filter1 =
Builders<PostDataItem>
.Filter
.Regex(
(fun n -> n.reference1 :> obj),
BsonRegularExpression("1002\d{5}"))
(恕我直言,这只是他们 API 中的一点 C# 偏见。希望他们有一天会修复它。)