繁体   English   中英

正则表达式选项“/ x”(忽略选项在MongoDB C#Driver中不起作用

[英]Regular Expression option “/x” (ignore option is not working in MongoDB C# Driver

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

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

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

但事实证明,为了使用“x”和“s”选项,你必须使用$ options:下面的mongoDB格式是MongoDB文档的确切引用

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

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

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

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM