本文介绍了如何使用正则表达式从C#中的MongoDB集合中获取数据?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在我的 MVC(C#) Web应用程序中使用 MongoDB.Drivers nuget软件包与MongoDB数据库进行通信.现在,我想基于特定的列及其值来获取数据.我使用下面的代码来获取数据.

I am using MongoDB.Drivers nuget package in my MVC (C#) web application to communication with MongoDB database. Now, I want to fetch data based on specific column and it's value. I used below code to fetch data.

var findValue = "John";
                var clientTest1 = new MongoClient("mongodb://localhost:XXXXX");
                var dbTest1 = clientTest1.GetDatabase("Temp_DB");
                var empCollection = dbTest1.GetCollection<Employee>("Employee");
                var builder1 = Builders<Employee>.Filter;
                var filter1 = builder1.Empty;
                var regexFilter = new BsonRegularExpression(findValue, "i");
                filter1 = filter1 & builder1.Regex(x => x.FirstName, regexFilter);
                filter1 = filter1 & builder1.Eq(x => x.IsDeleted,false);
                var collectionObj = await empCollection.FindAsync(filter1);
                var dorObj = collectionObj.FirstOrDefault();

但是,上面的代码正在执行类似 like 的查询.这意味着它正在作为( select * from Employee where FirstName like'%John%')而不是我想要的.我只想获取其 FirstName 值应完全匹配的那些数据.(例如,在这种情况下,FirstName应该等于 John ).

But, the above code is performing like query.It means it is working as (select * from Employee where FirstName like '%John%') I don't want this. I want to fetch only those data whose FirstName value should match exact. (like in this case FirstName should equal John).

我该如何执行此操作,任何人都可以向我提出建议.

How can I perform this, can anyone provide me suggestions on this.

注意::我使用了 new BsonRegularExpression(findValue,"i")进行搜索,使该代码不区分大小写.

Note: I used new BsonRegularExpression(findValue, "i") to make search case-insensitive.

任何帮助将不胜感激.

谢谢

推荐答案

我建议存储数据的规范化版本,并对其进行索引/搜索.它可能比使用正则表达式快得多.当然,通过在"John"旁边加上"john"会占用更多的存储空间,但是由于您只能使用标准的$ eq查询,因此您的数据访问速度会更快.

I would recommend storing a normalized version of your data, and index/search upon that. It will likely be considerably faster than using regex. Sure, you'll eat up a little more storage space by including "john" alongside "John", but your data access will be faster since you would just be able to use a standard $eq query.

如果您坚持使用正则表达式,建议您在搜索词前后使用 ^ (行首)和 $ (行尾).不过请记住,您应该逃避发现值,以免将其内容视为RegEx.

If you insist on regex, I recommend using ^ (start of line) and $ (end of line) around your search term. Remember though, that you should escape your find value so that its contents isn't treated as RegEx.

这应该有效:

string escapedFindValue = System.Text.RegularExpressions.Regex.Escape(findValue);
new BsonRegularExpression(string.Format("^{0}$", escapedFindValue), "i");

或者,如果您使用的是较新的框架版本,则可以使用字符串插值:

Or if you're using a newer framework version, you can use string interpolation:

string escapedFindValue = System.Text.RegularExpressions.Regex.Escape(findValue);
new BsonRegularExpression($"^{escapedFindValue}$", "i");

这篇关于如何使用正则表达式从C#中的MongoDB集合中获取数据?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-31 07:02