【问题标题】:Could not find the matching Number in mongodb aggregation using Regex使用正则表达式在 mongodb 聚合中找不到匹配的数字
【发布时间】:2016-10-04 06:45:37
【问题描述】:

我有一个收藏

//演示名称:user_informations

  {
        "_id" : 3015,
        "Phone Number" : "9159571195",
        "First Name" : "logesh",
        "Last Name" : "chandiran",
        "Email ID" : "logu@gmail.com",
        "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"}}}

         ])

上述查询适用于字符串中的值,但不适用于返回为 null 的数字。如何将值与集合字段中的数字匹配。 我需要从集合中匹配数据的计数和结果。

【问题讨论】:

    标签: node.js regex mongodb mongodb-query aggregation-framework


    【解决方案1】:
    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 }
        }})
    

    【讨论】:

      【解决方案2】:

      Regex 是一种用于字符串而非数字的工具。使用常规数字比较运算符,如$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"}
          }})
      

      希望所有括号都到位。

      【讨论】:

      • 有没有办法匹配数字中的数字,比如输入2返回200数字
      猜你喜欢
      • 1970-01-01
      • 2020-11-02
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-11-13
      • 2022-01-18
      • 2014-09-27
      • 1970-01-01
      相关资源
      最近更新 更多