$strLenBytes
The $strLenBytes operator in Amazon DocumentDB is used to determine the length of a string in bytes. This is useful when you need to understand the storage size of a string field, especially when dealing with Unicode characters that may use more than one byte per character.
Parameters
-
expression: The string expression to calculate the length of.
Example (MongoDB Shell)
This example demonstrates how to use the $strLenBytes operator to calculate the length of string fields in bytes.
Create sample documents
db.people.insertMany([ { "_id": 1, "Desk": "Düsseldorf-BVV-021" }, { "_id": 2, "Desk": "Munich-HGG-32a" }, { "_id": 3, "Desk": "Cologne-ayu-892.50" }, { "_id": 4, "Desk": "Dortmund-Hop-78" } ]);
Query example
db.people.aggregate([ { $project: { "Desk": 1, "length": { $strLenBytes: "$Desk" } } } ])
Output
{ "_id" : 1, "Desk" : "Düsseldorf-BVV-021", "length" : 19 }
{ "_id" : 2, "Desk" : "Munich-HGG-32a", "length" : 14 }
{ "_id" : 3, "Desk" : "Cologne-ayu-892.50", "length" : 18 }
{ "_id" : 4, "Desk" : "Dortmund-Hop-78", "length" : 15 }
Note that the length of the "Düsseldorf-BVV-021" string is 19 bytes, which is different from the number of code points (18) due to the Unicode character "Ü" occupying 2 bytes.
Code examples
To view a code example for using the $strLenBytes command, choose the tab for the language that you want to use: