Regex string compare not working in $redact stage in Aggregation query

    • Type: Bug
    • Resolution: Unresolved
    • Priority: Major - P3
    • None
    • Affects Version/s: 7.0.28, 8.2.4
    • Component/s: None
    • ALL
    • Hide

      Lets say following document is present in MongoDB collection

      {
          "ObjArray" : [ 
              {
                  "boolOrStringbool" : true,
                  "type" : "ClassA"
              }, 
              {
                  "boolOrStringbool" : "true",
                  "type" : "ClassA"
              }
          ]
      }

       

      Now using $redact stage in aggregation query to reduce objects in the array. Following query should ideally not redact any object.

       

      db.getCollection('regex_bug').aggregate( [
      {$redact: {
          $cond: {
              if: {
                 $or: [{ $not: "$type" },
                       {
                        $cond: {
                          if: { $eq: [{$type: "$boolOrStringbool"}, "string"]},                         then: { $eq: ["$boolOrStringbool", /true/i]},                                            else: {$eq: ["$boolOrStringbool", true]}
                        }
                       }
                      ]
               },
               then: "$$DESCEND",
               else: "$$PRUNE"
             }
           }
         }
      ]

       
      doesn't filter the array properly. I get the following result
       

      {
          "_id" : ObjectId("6985ee7994b54ed76c1dd9f7"),
          "ObjArray" : [ 
              {
                  "boolOrStringbool" : true,
                  "type" : "ClassA"
              }
          ]
      }

       
       
      If we do not use regex for comparing string then it works properly
       

      db.getCollection('regex_bug').aggregate( [
      {$redact: {
         $cond: {
           if: {
            $or: [{ $not: "$type" },
                  {
                    $cond: {
                      if: { $eq: [{$type: "$boolOrStringbool"}, "string"]},                         then: { $eq: ["$boolOrStringbool", "true"]},                                  
                      else: {$eq: ["$boolOrStringbool", true]}
                    }
                  }
              ]
             },
             then: "$$DESCEND",
             else: "$$PRUNE"
            }
          }
        }
      ]

       
      gives the following output:
       

      {
          "_id" : ObjectId("6985ee7994b54ed76c1dd9f7"),
          "ObjArray" : [ 
              {
                  "boolOrStringbool" : true,
                  "type" : "ClassA"
              }, 
              {
                  "boolOrStringbool" : "true",
                  "type" : "ClassA"
              }
          ]
      }

      Note: In our database the original document is highly nested and complex and we need to use $redact only to filter the arrays.

      Show
      Lets say following document is present in MongoDB collection {     "ObjArray" : [          {             "boolOrStringbool" : true ,             "type" : "ClassA"         },          {             "boolOrStringbool" : " true " ,             "type" : "ClassA"         }     ] }   Now using $redact stage in aggregation query to reduce objects in the array. Following query should ideally not redact any object.   db.getCollection( 'regex_bug' ).aggregate( [ {$redact: { $cond: { if : {       $or: [{ $not: "$type" },             { $cond: {                 if : { $eq: [{$type: "$boolOrStringbool" }, "string" ]},                 then: { $eq: [ "$boolOrStringbool" , / true /i]},                 else : {$eq: [ "$boolOrStringbool" , true ]}               }             }           ] },         then: "$$DESCEND" ,         else : "$$PRUNE"       }     }   } ]   doesn't filter the array properly. I get the following result   {     "_id" : ObjectId( "6985ee7994b54ed76c1dd9f7" ),     "ObjArray" : [          {             "boolOrStringbool" : true ,             "type" : "ClassA"         }     ] }     If we do not use regex for comparing string then it works properly   db.getCollection('regex_bug').aggregate( [ {$redact: { $cond: { if: {       $or: [{ $not: "$type" },             { $cond: {                 if: { $eq: [{$type: "$boolOrStringbool"}, "string"]},                 then: { $eq: ["$boolOrStringbool", "true"]},                 else: {$eq: ["$boolOrStringbool", true]}               }             }         ] },        then: "$$DESCEND",        else: "$$PRUNE"       }     }   } ]   gives the following output:   {     "_id" : ObjectId( "6985ee7994b54ed76c1dd9f7" ),     "ObjArray" : [          {             "boolOrStringbool" : true ,             "type" : "ClassA"         },          {             "boolOrStringbool" : " true " ,             "type" : "ClassA"         }     ] } Note: In our database the original document is highly nested and complex and we need to use $redact only to filter the arrays.
    • None
    • None
    • None
    • None
    • None
    • None
    • None

      While trying to redact documents in an array based on the value of an attribute which can be either string or a boolean fails for string comparison when a regex is used.

            Assignee:
            Sahachel Flores
            Reporter:
            Mandeep Singh
            Votes:
            0 Vote for this issue
            Watchers:
            1 Start watching this issue

              Created:
              Updated: