c# mongodb or查询,C#mongodb区分大小写的搜索

I have a collection in which I store Email and password of user.

I obviously don't want to require the user to insert his email case sensitive and to be exactly as when he first registered.

I'm using mongodb 2.0 c# driver, I'm repeating it because I saw solutions to queries written with regex but I'm afraid I cant user it in here.

my query looks like

var filter = Builders.Filter.And(

Builders.Filter.Eq(u => u.Email, email),

Builders.Filter.Eq(u => u.Password, password));

ME_User foundUser = null;

var options = new FindOptions

{

Limit = 1

};

using (var cursor = await manager.User.FindAsync(filter, options))

{

while (await cursor.MoveNextAsync())

{

var batch = cursor.Current;

foreach (ME_User user in batch)

foundUser = user;

}

}

I have an issue with disorder, kill me, but I cant allow myself save this data again with lower case and have 2 copies of the same thing. Also, I want the email to be saved EXACTLY like the user inserted it.

解决方案

Filtering on string fields in Mongodb is case sensitive without using regular expressions. Why exactly you cannot use regular expressions?

Your query can be edited like this:

var filter = Builders.Filter.And(

Builders.Filter.Regex(u => u.Email, new BsonRegularExpression("/^" + email + "$/i"),

Builders.Filter.Eq(u => u.Password, password));

Notice the "^" and "$" signs to specify a complete word search and most important the case-insensitive operator at the end of the regular expression ("/i").

Another way vould be the Text search, that requires the creation of a text index and is case insensitive for latin alphabet: http://docs.mongodb.org/manual/reference/operator/query/text/#match-operation

In C#, you will use with the Text Filter:

var filter = Builders.Filter.And(

Builders.Filter.Text(email),

Builders.Filter.Eq(u => u.Password, password));

With a text index query in an OR clause, you will need to create an index on Password field as well, otherwise the OR query will produce an error:

Other non-TEXT clauses under OR have to be indexed as well

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
操作monodb的c#封装,调用非常方便,可以继承,功能包括: 1、所有数据库操作 2、前台表格类数据获取 public List GetList(List lstColName, Document query, JqGridParam jqParam, ref int count),封装了通用的获取前台表格数据的方法,将在工程中减少大量数据库访问代码,有了这个后对前台表格类查询我们可以不用在Control里使用linq或者封装在Model里然后对前台定义视图类了,使用如下: try { JqGridParam jqParam = new JqGridParam(); jqParam.page = 1; jqParam.rows = 1000; MemberOper memOper = new MemberOper(); MongoBasicOper monOper = new MongoBasicOper(DTName.GROUP_MEMBER); int count = 0; //过滤条件 Document query = new Document(); if (!string.IsNullOrEmpty(find)) { MongoRegex reg = new MongoRegex(".*" + find + ".*"); query.Add(DColName.Name, reg); } query.Add(DColName.GroupId, g); Document[] docStatus = new Document[] { new Document(DColName.Status, RowStatus.Pass), new Document(DColName.Status, RowStatus.Admin) }; query.Add("$or", docStatus); //查询列 List lstColName = new List(); lstColName.Add(DColName.UserId); lstColName.Add(DColName.UserName); //查询数据 var lstRes = monOper.GetListEx(lstColName, query, jqParam, ref count); //转换返回值 JqGrid jg = new JqGrid(); if (count == 0) { return Json(jg.toNull(jqParam), JsonRequestBehavior.AllowGet); } var jsonData = jg.toJson(jqParam, count, lstRes, lstColName); jsonData.param = g; return Json(jsonData, JsonRequestBehavior.AllowGet); } catch (Exception e) { return Json(e.Message, JsonRequestBehavior.AllowGet); }

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值