简体   繁体   中英

Filter array elements with $regex

 ///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
        },
 ]
}

Expected Output : entire data that matches particular string in search box from "services" field.

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

I am facing an issue in regex portion in the above Collection, services is an array field. Please help me to filter the data.

This is because you are passing in a string of JavaScript regular expression object to $regex . Change your regex to one of the following.

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

or

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

Guys since i am new to Mongodb it took me a day to find a proper solution to my task. I have a solution to my issue. If you guys have better query than this, just post it or modify it....

 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"}
               }        
])

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM