假设我的mongodb数据使用int
存储一些数据现在我想用正则表达式查询聚合 那些num以85开头,我如何用聚合函数做到这一点?
答案 0 :(得分:1)
$substr can be used in a $project stage to convert the num to a string value. You can then have a $match stage with the regex.
Project Stage:
{
$project:{
numAsString: { $substr : ["$num", 0, -1 ] }
}
}
Match Stage:
{
$match : { numAsString: { $regex: /^85.*/ } }
}
答案 1 :(得分:1)
Although this can be done by regexes, I would like to suggest an alternate method. The problem with regex is that it wont allow you to index elements. Hence as your collection size increases, your queries will become slower and slower.
You can just go for the basics and do the following checks
public string Method()
{
return Request.Browser.MajorVersion.ToString();
}
Keep doing this till you reach the max possible value in {$or: [
{$and: [ {$gte: ['$num', 85] }, {$lte: ['$num', 85] } ]},
{$and: [ {$gte: ['$num', 850] }, {$lte: ['$num', 859] } ]},
{$and: [ {$gte: ['$num', 8500] }, {$lte: ['$num', 8599] } ]},
{$and: [ {$gte: ['$num', 85000] }, {$lte: ['$num', 85999] } ]},
{$and: [ {$gte: ['$num', 850000] }, {$lte: ['$num', 859999] } ]},
{$and: [ {$gte: ['$num', 8500000] }, {$lte: ['$num', 8599999] } ]},
{$and: [ {$gte: ['$num', 85000000] }, {$lte: ['$num', 85999999] } ]},
{$and: [ {$gte: ['$num', 850000000] }, {$lte: ['$num', 859999999] } ]},
{$and: [ {$gte: ['$num', 8500000000] }, {$lte: ['$num', 8599999999] } ]},
{$and: [ {$gte: ['$num', 85000000000] }, {$lte: ['$num', 85999999999] } ]},
]}
. Sorry for the ugly code, but it should run faster.
答案 2 :(得分:1)
First you have to convert integer in to string using $substr operator than perform $match funtion using $regex operator
<div class="EditBoking">
<div class="title">
Din nuvarande bokning
</div>
<div>
<ul class="listOneRow">
<li>
<span>Startdatum: 2017-12-12</span>
</li>
<li>
<span>Slutdatum: 2017-12-12</span>
</li>
<li>
<span>Program: some program</span>
</li>
<li>
<span>Miljö: Arrendator</span>
</li>
</ul>
</div>
</div>
答案 3 :(得分:1)
在特定情况下使用正则表达式毫无意义。正如其他人已经指出的那样,可以使用$substr完成对字符串的转换,但过滤所需的全部是标准等于比较:
public int compare(T o1, T o2)