2016-10-04 93 views
0

MongoDB中聚集匹配数我有一个集合找不到使用正则表达式

//演示名称:user_informations

{ 
     "_id" : 3015, 
     "Phone Number" : "9159571195", 
     "First Name" : "logesh", 
     "Last Name" : "chandiran", 
     "Email ID" : "[email protected]", 
     "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"}}} 

     ]) 

的上面的查询可以很好地处理字符串中的值,但不会与数字返回为空。如何将值与收集字段中的数字进行匹配。 我需要从集合中匹配数据的计数和结果。

回答

0
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 } 
    }}) 
0

正则表达式是字符串,而不是数字的工具。使用正则数比较运营商,像$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"} 
    }}) 

希望,所有的括号到位。

+0

谢谢你的朋友 –

+0

有没有什么办法可以像输入2那样匹配数字的位数返回200的数字 –