【问题标题】:Filter array elements with $regex使用 $regex 过滤数组元素
【发布时间】:2016-09-26 13:38:35
【问题描述】:
 ///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
        },
 ]
}

预期输出:与“服务”字段中搜索框中的特定字符串匹配的整个数据。

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

我在上述集合中的正则表达式部分遇到问题,services 是一个数组字段。请帮我过滤数据。

【问题讨论】:

  • stackoverflow.com/questions/16252208/… 这可能对你有帮助
  • 您尝试与正则表达式匹配的嵌入式文档字段是什么?
  • 您文档中的搜索框是什么?您不能将正则表达式应用于数组 您想要将正则表达式应用到的子文档中的字段名称是什么?
  • 我只想将数据与服务中的所有字段匹配并在表格中显示输出

标签: regex mongodb mongodb-query aggregation-framework


【解决方案1】:

伙计们,因为我是 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"}
               }        
])

【讨论】:

    【解决方案2】:

    这是因为您将 JavaScript 正则表达式对象的字符串传递给 $regex。将您的正则表达式更改为以下之一。

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

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

    【讨论】:

    • 仍然收到错误“管道阶段规范对象必须包含一个字段”。将正则表达式传递给数组字段是否有任何问题。
    • 请使用示例文档和预期结果编辑您的问题。
    • 它也试过这个 db.collections.aggregate([ {$match:{"Phone Number" : "9159571195","services":{"$regex": /^E/, " $options": "si"}}}, { $project: { Number: '$Phone Number', Name: '$services' } } ])
    猜你喜欢
    • 1970-01-01
    • 2014-01-05
    • 2017-01-04
    • 2021-01-01
    • 1970-01-01
    • 2012-09-17
    • 2016-09-04
    • 2013-02-26
    • 2019-12-20
    相关资源
    最近更新 更多