2016-09-26 38 views
-1
///sample Data 
{ 
    "_id" : "CUST1234", 
    "Phone Number" : "9585290750", 
    "First Name" : "jeff", 
    "Last Name" : "ayan", 
    "Email ID" : "", 
    "createddate" : 1462559400000.0, 
    "services" : [ 
     { 
      "type" : "Enquiry", 
      "timeSpent" : "0:00", 
      "trxID" : "TRXE20160881", 
      "CustomerQuery" : "Enquiry about travell agent numbers in basaveshwara nagara", 
      "ServiceProvided" : "provided info through whatsapp", 
      "Category" : "Tours/Travels", 
      "callTime" : "2016-05-06T18:30:00.000Z", 
      "ActualAmount" : 0, 
      "FinalAmount" : 0, 
      "DiscountRuppes" : 0, 
      "DiscountPerctange" : 0 
     }, 
     { 
      "type" : "Enquiry", 
      "timeSpent" : "0:00", 
      "trxID" : "TRXE20160882", 
      "CustomerQuery" : "Enquiry about Electric bill payment of house", 
      "ServiceProvided" : "Service provided", 
      "Category" : "Utility Services", 
      "callTime" : "2016-05-10T18:30:00.000Z", 
      "ActualAmount" : 0, 
      "FinalAmount" : 0, 
      "DiscountRuppes" : 0, 
      "DiscountPerctange" : 0 
     }, 
     { 
      "type" : "Enquiry", 
      "timeSpent" : "0:00", 
      "trxID" : "TRXE20160883", 
      "CustomerQuery" : "Enquiry about KPSC office number", 
      "ServiceProvided" : "provided info through whatsapp", 
      "Category" : "Govt Offices/Enquiries", 
      "callTime" : "2016-05-13T18:30:00.000Z", 
      "ActualAmount" : 0, 
      "FinalAmount" : 0, 
      "DiscountRuppes" : 0, 
      "DiscountPerctange" : 0 
     }, 
     { 
      "type" : "Enquiry", 
      "timeSpent" : "0:00", 
      "trxID" : "TRXE20160884", 
      "CustomerQuery" : "Enquiry about Sagara appolo hospital contact number", 
      "ServiceProvided" : "provided the information through call", 
      "Category" : "Hospitals/Equipments", 
      "callTime" : "2016-05-14T18:30:00.000Z", 
      "ActualAmount" : 0, 
      "FinalAmount" : 0, 
      "DiscountRuppes" : 0, 
      "DiscountPerctange" : 0 
     }, 
] 
} 

預期輸出:與「services」字段中的搜索框中的特定字符串匹配的完整數據。

 db.collection.aggregate([ 
     { 
      $match: { 
       "Phone Number": "9585290750", 
       "services": { $regex: "/^t/", $options: "s i" } 
      } 
     },      
     { 
      $project: { 
       "Services": "services" 
      } 
     } 
    ]); 

我對着在上述集合部的正則表達式的問題,services是一個數組字段。請幫我過濾數據。

+1

http://stackoverflow.com/questions/16252208/how-to-use-regex-in-mongodb-aggregation -query-within-match這可能會幫助你 – Panky031

+0

你試圖與你的正則表達式匹配的嵌入式文檔字段是什麼? – styvane

+0

什麼是文檔中的*搜索框?您不能將正則表達式應用到數組中什麼是您想要應用正則表達式的子文檔中的字段名稱? – styvane

回答

0

傢伙,因爲我是新來的MongoDB,我花了一天才找到我的任務,妥善的解決辦法。我有解決我的問題。如果你們有更好的查詢比這,只是張貼或修改....

db.collections.aggregate([ 
     {"$match":{"Corporate_ID":"id"}}, 
     {"$unwind":"$services"}, 
     {"$match":{"$or":[ 
      {"services.type":{$regex:'TRXF2016088142',"$options": "i"}}, 
      {"services.timeSpent":{$regex:'TRXF2016088142',"$options": "i"}}, 
      {"services.trxID":{$regex:'TRXF2016088142',"$options": "i"}}, 
      {"services.CustomerQuery":{$regex:'F',"$options": "i"}}, 
      {"services.ServiceProvided":{$regex:'F',"$options": "i"}}, 
      {"services.Category":{$regex:'F',"$options": "i"}}, 
      {"services.callTime":{$regex:'TRXF2016088142',"$options": "i"}}, 
      {"services.ActualAmount":{$regex:'TRXF2016088142',"$options": "i"}}, 
      {"services.FinalAmount":{$regex:'TRXF2016088142',"$options": "i"}}, 
      {"services.DiscountRuppes":{$regex:'TRXF2016088142',"$options": "i"}}, 
      {"services.DiscountPerctange":{$regex:'TRXF2016088142',"$options": "i"}}      
      ]}}, 
     {"$unwind":"$services"}, 
     {"$project":{ 
      "service":"$services"} 
       }   
]) 
0

這是因爲您正在傳遞一個JavaScript正則表達式對象的字符串到$regex。將您的正則表達式更改爲以下內容之一。

"service": { "$regex": /^t/, "$options": "si" } 

"service": { "$regex": "^t", "$options": "si" } 
+0

仍然出現錯誤「流水線階段規範對象必須只包含一個字段」。將正則表達式傳遞給數組字段有任何問題。 –

+0

請編輯你的問題樣本文件和預期的結果。 – styvane

+0

它也試過這個db.collections.aggregate($ {match:{「Phone Number」:「9159571195」,「services」:{「$ regex」:/^E /,「$ options」:「si 「}}}, {$ project:{ Number:'$ Phone Number', Name:'$ services' } } ]) –