![](/img/trans.png)
[英]How to find number of occurrence of searched string in mongodb aggregation?
[英]Could not find the matching Number in mongodb aggregation using Regex
我有一个收藏
//演示名称:user_informations
{
"_id" : 3015,
"Phone Number" : "9159571195",
"First Name" : "logesh",
"Last Name" : "chandiran",
"Email ID" : "logu@gmail.com",
"Gender" : "male",
"customerID" : "CUST3015",
"createddate" : 1472482064363.0,
"modifieddate" : 1474384997049.0,
"transationHistory" : [
{
"transactionStatus" : "Success",
"transactionAmount" : 2500,
"paymentId" : "Q8urVX9Cpf",
"transactionDate" : 1472754600000.0,
"transactionId" : "7egsOpmqBR",
"comments" : "EMI",
"medium" : "Cash"]
}
}
我正在使用以下查询
db.user_informations.aggregate([
{
$match:{"Phone Number": '9159571195'}},
{$unwind:'$transationHistory'},
{"$match":{"$or":[
{"transationHistory.transactionAmount":{$regex:/2500/i}},
{"transationHistory.transactionId":{$regex:/2500/i}},
{"transationHistory.paymentId":{$regex:/2500/i}},
{"transationHistory.transactionStatus":{$regex:/2500/i}},
{"transationHistory.medium":{$regex:/2500/i}},
{"transationHistory.comments":{$regex:/2500/i}}
]}},
{"$group":{_id: null,"count":{$sum: 1 },"result":{$push:"$transationHistory"}}}
])
上面的查询适用于字符串中的值,但不适用于返回为null的数字。 如何将值与收集字段中的数字匹配。 我需要从集合中匹配数据的计数和结果。
正则表达式是用于字符串而非数字的工具。 使用常规的数字比较运算符,例如$eq
, $gt
, $lt
等。
基本上,它将是:
db.user_informations.aggregate(
{$match:{"Phone Number": '9159571195'}},
{$unwind:'$transationHistory'},
{"$match":{
"$or":[
{"transationHistory.transactionAmount":{$eq:2500}}
]
},
{"$group":{
_id: null,
"count":{$sum: 1 },
"result":{$push:"$transationHistory"}
}})
希望所有支架都到位。
db.user_informations.aggregate(
{$match:{"Phone Number": '9159571195'}},
{$unwind:'$transationHistory'},
{$project:{
"transactionAmount":{ "$toLower":"$transationHistory.transactionAmount"}
}},
{"$match":{
"$or":[
{"transactionAmount":{$regex:txt,"$options": "i"}}
]
},
{"$group":{
_id: null,
"count":{$sum: 1 }
}})
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.