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 }
|