【问题标题】:Regular Expression option "/x" (ignore option is not working in MongoDB C# Driver正则表达式选项“/x”(忽略选项在 MongoDB C# 驱动程序中不起作用
【发布时间】:2017-03-17 10:39:52
【问题描述】:

我必须在我的应用程序中执行查找/替换功能,我们决定在正则表达式 Mongo 查询中添加忽略空格选项。所以使用这段代码

 BsonRegularExpression  breg = new BsonRegularExpression (pattern,request.matchCase ? "x" : "xi");

它产生这个字符串,稍后在 mongo 中查询 {/patter/xi}

但事实证明,要使用“x”和“s”选项,您必须使用 $options: 下面 mongoDB 中的格式是来自 MongoDB Documentation 的确切引用

要使用 x 选项或 s 选项,您必须将 $regex 运算符表达式与 $options 运算符一起使用。例如,要指定 i 和 s 选项,您必须同时使用 $options

所以现在我不知道如何使 MongoDB C# 驱动程序生成我想要的查询,或者是否有任何其他解决方法。

【问题讨论】:

    标签: c# regex mongodb mongodb-.net-driver


    【解决方案1】:

    这是我知道的两种可能性。

    var spec = new Document("someName", new MongoRegex("your regex", "x"));
    yourCollection.Find(spec)
    

    第二种方法

    var collection = GetCollection();
        var filter = Builders<Entity>.Filter.Regex("x", new BsonRegularExpression("your regex, "i"));
        return await collection.Find(filter).FirstOrDefaultAsync();
    

    有关 BsonRegularExpression 方法的更多信息,请查看http://api.mongodb.com/java/current/org/bson/BsonRegularExpression.html

    【讨论】:

    • 新文件?是 Bson 文档还是什么类?
    • 我正在使用地图类,所以我这样做了FieldDefinition &lt;Step_StepContentNSDoc&gt; StepHTML = "StepHTML"; var filter = Builders&lt;Step_StepContentNSDoc&gt;.Filter.Regex(StepHTML, new BsonRegularExpression(pattern, "ix")); var collec = procAgg.Collection.Database.GetCollection&lt;ProcedureNSDoc&gt;("procedure"); 但 collec.find 不接受过滤器只接受 mongo 查询
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2019-09-23
    • 1970-01-01
    • 2015-02-15
    • 1970-01-01
    • 1970-01-01
    • 2016-03-15
    相关资源
    最近更新 更多