2016-10-04 72 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的數字 –