Uploaded image for project: 'C# Driver'
  1. C# Driver
  2. CSHARP-1660

Add expressions to determine the length of a string

    • Type: Icon: Improvement Improvement
    • Resolution: Done
    • Priority: Icon: Major - P3 Major - P3
    • 2.4
    • Affects Version/s: None
    • Component/s: Linq
    • None

      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 }
      

            Assignee:
            craig.wilson@mongodb.com Craig Wilson
            Reporter:
            rathi.gnanasekaran Rathi Gnanasekaran
            Votes:
            0 Vote for this issue
            Watchers:
            1 Start watching this issue

              Created:
              Updated:
              Resolved: