问题描述
使用"电话号码"以"1234567890"格式具有BsonDocument
集合.此SQL会获取所有区号在300到399之间的电话号码;
Have an BsonDocument
collection with PhoneNumber in the "1234567890" format. This SQL gets all the PhoneNumbers with the Area Codes between 300 and 399;
WHERE (LEFT(PhoneNumber,3) BETWEEN '300' AND '399')
我将如何在MongoDB中做到这一点?如果可能的话,我想使用MongoDB.Driver.Builders
.
How would I do this in MongoDB? I would like to use the MongoDB.Driver.Builders
if possible.
推荐答案
如果您只想从数字"3"开始的电话号码,则可以使用 @mstearn 的明智决定,这里只是c#实现:
If you just want phone number that's starts from number '3' you can just use smart decision of @mstearn, here just c# realization:
var query = Query.EQ("PhoneNumber", new BsonRegularExpression("^3"));
但是可以说如果您需要查询345-369范围内的前三个数字以使其起作用(没有慢速运算符:$where
,$regex
),则可以创建其他字段并在那里存储前三个数字(区域代码) )的电话.然后使用 @yi _H提出的查询,这里再次是c#驱动程序实现:
But lets say if you need query first 3 numbers in range 345 -- 369 to make it work (without slow operators: $where
, $regex
) you can create additional field and store there first 3 numbers (area code) of phone. And then use query proposed by @yi_H , here again c# driver realization:
var query = Query.GTE("PhoneAreaCode", 345).LTE(369);
不在乎mongodb中的 extra 字段-这是常见的做法.通常,额外字段的工作速度比查询期间的任何计算都要快.
Don't care about extra field in mongodb -- it's common practice. Extra fields usual working faster than any calculation during querying.
这篇关于构建此LEFT()"SQL";在MongoDB查询?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!