$strLenBytes - Amazon DocumentDB

$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:

Node.js
const { MongoClient } = require('mongodb'); async function example() { const client = await MongoClient.connect('mongodb://<username>:<password>@<cluster-endpoint>:27017/?tls=true&tlsCAFile=global-bundle.pem&replicaSet=rs0&readPreference=secondaryPreferred&retryWrites=false'); const db = client.db('test'); const collection = db.collection('people'); const result = await collection.aggregate([ { $project: { "Desk": 1, "length": { $strLenBytes: "$Desk" } } } ]).toArray(); console.log(result); client.close(); } example();
Python
from pymongo import MongoClient def example(): client = MongoClient('mongodb://<username>:<password>@<cluster-endpoint>:27017/?tls=true&tlsCAFile=global-bundle.pem&replicaSet=rs0&readPreference=secondaryPreferred&retryWrites=false') db = client.test collection = db.people result = list(collection.aggregate([ { '$project': { "Desk": 1, "length": { "$strLenBytes": "$Desk" } } } ])) print(result) client.close() example()