Uploaded image for project: 'Core Server'
  1. Core Server
  2. SERVER-14670

Add expressions to determine the length of a string

    XMLWordPrintable

    Details

      Description

      Syntax

      {$strLenBytes: <expression>}
      {$strLenCP: <expression>}  // CP stands for UTF-8 'code points'.
      

      Examples

      Input

      {_id: 0, string: "cliché"}
      

      Pipeline

      db.coll.aggregate([{
          $project: {
              byteLength: {$strLenBytes: "$string"},
              cpLength: {$strLenCP: "$string"}
          }
      }])
      

      Output

      {_id: 0, byteLength: 7, cpLength: 6}
      

      Original Description

      We need to determine string length in aggregation pipeline

      eg:

      db.kol.insert({"text": "abcde"})
      db.kol.insert({"text": "ab"})
       
      db.kol.aggregate({ $project: { "text_length": {$length: "$text"  }}})
      

      result:

      { "_id" : ObjectId("53d0c9bdc2644cdc0ab835f5"), "text_length" : 5 }
      { "_id" : ObjectId("53d0c9c1c2644cdc0ab835f6"), "text_length" : 2 }
      

        Attachments

          Issue Links

            Activity

              People

              Assignee:
              benjamin.murphy Benjamin Murphy
              Reporter:
              falsecz Lukas Benes
              Participants:
              Votes:
              15 Vote for this issue
              Watchers:
              19 Start watching this issue

                Dates

                Created:
                Updated:
                Resolved: