MongoDB $strLenCP

MongoDB, the $strLenCP aggregation pipeline operator returns the number of UTF-8 code points in the specified string.

The $strLenCP operator is different to the $strLenBytes operator, which returns the number of bytes in the string.

Example

Suppose we have a collection called english with the following documents:

{ "_id" : 1, "data" : "Maimuang" }
{ "_id" : 2, "data" : "M" }
{ "_id" : 3, "data" : "a" }
{ "_id" : 4, "data" : "i" }
{ "_id" : 5, "data" : "m" }
{ "_id" : 6, "data" : "u" }
{ "_id" : 7, "data" : "a" }
{ "_id" : 8, "data" : "n" }
{ "_id" : 9, "data" : "g" }

We can apply $strLenCP to the data field in those documents:

db.english.aggregate(
   [
     {
       $project:
          {
            _id: 0,
            data: 1,
            result: { $strLenCP: "$data" }
          }
     }
   ]
)

Result:

{ "data" : "Maimuang", "result" : 8 }
{ "data" : "M", "result" : 1 }
{ "data" : "a", "result" : 1 }
{ "data" : "i", "result" : 1 }
{ "data" : "m", "result" : 1 }
{ "data" : "u", "result" : 1 }
{ "data" : "a", "result" : 1 }
{ "data" : "n", "result" : 1 }
{ "data" : "g", "result" : 1 }

We can see that the whole word uses 8 code points and each character uses one code point.

Thai Characters

Here’s an example that uses Thai characters, which are 3 bytes each, but use just one code point.

We have a collection called thai with the following documents:

{ "_id" : 1, "data" : "ไม้เมือง" }
{ "_id" : 2, "data" : "ไ" }
{ "_id" : 3, "data" : "ม้" }
{ "_id" : 4, "data" : "เ" }
{ "_id" : 5, "data" : "มื" }
{ "_id" : 6, "data" : "อ" }
{ "_id" : 7, "data" : "ง" }

And here’s what happens when we apply $strLenCP to those documents:

db.thai.aggregate(
   [
     {
       $project:
          {
            _id: 0,
            data: 1,
            result: { $strLenCP: "$data" }
          }
     }
   ]
)

Result:

{ "data" : "ไม้เมือง", "result" : 8 }
{ "data" : "ไ", "result" : 1 }
{ "data" : "ม้", "result" : 2 }
{ "data" : "เ", "result" : 1 }
{ "data" : "มื", "result" : 2 }
{ "data" : "อ", "result" : 1 }
{ "data" : "ง", "result" : 1 }

Two of these characters have been modified using diacritics, which result in 2 code points being returned. These characters return 6 bytes when using the $strLenBytes operator.

Other Characters

Suppose we have a collection called other with the following documents:

{ "_id" : 1, "data" : "é" }
{ "_id" : 2, "data" : "©" }
{ "_id" : 3, "data" : "℘" }

And let’s apply $strLenCP to those documents:

db.other.aggregate(
   [
     { $match: { _id: { $in: [ 1, 2, 3 ] } } },
     {
       $project:
          {
            _id: 0,
            data: 1,
            result: { $strLenCP: "$data" }
          }
     }
   ]
)

Result:

{ "data" : "é", "result" : 1 }
{ "data" : "©", "result" : 1 }
{ "data" : "℘", "result" : 1 }

Each of these characters use a single code point (even though such characters use more than one byte).

The space character uses a code point. Two space characters therefore use 2 code points, and so on.

Suppose we have the following documents:

{ "_id" : 4, "data" : " " }
{ "_id" : 5, "data" : "  " }

And we apply $strLenCP to those documents:

db.other.aggregate(
   [
     { $match: { _id: { $in: [ 4, 5 ] } } },
     {
       $project:
          {
            _id: 0,
            data: 1,
            result: { $strLenCP: "$data" }
          }
     }
   ]
)

Result:

{ "data" : " ", "result" : 1 }
{ "data" : "  ", "result" : 2 }

Empty Strings

Empty strings return 0.

Here’s a document with an empty string:

{ "_id" : 6, "data" : "" }

And here’s what happens when we apply $strLenCP to that document:

db.other.aggregate(
   [
     { $match: { _id: { $in: [ 6 ] } } },
     {
       $project:
          {
            _id: 0,
            data: 1,
            result: { $strLenCP: "$data" }
          }
     }
   ]
)

Result:

{ "data" : "", "result" : 0 }

Wrong Data Type

Passing the wrong data type results in an error.

Suppose we have the following document:

{ "_id" : 7, "data" : 123 }

The data field contains a number.

Let’s apply $strLenCP to that document:

db.other.aggregate(
   [
     { $match: { _id: { $in: [ 7 ] } } },
     {
       $project:
          {
            _id: 0,
            data: 1,
            result: { $strLenCP: "$data" }
          }
     }
   ]
)

Result:

uncaught exception: Error: command failed: {
	"ok" : 0,
	"errmsg" : "$strLenCP requires a string argument, found: double",
	"code" : 34471,
	"codeName" : "Location34471"
} : aggregate failed :
_getErrorWithCode@src/mongo/shell/utils.js:25:13
doassert@src/mongo/shell/assert.js:18:14
_assertCommandWorked@src/mongo/shell/assert.js:639:17
assert.commandWorked@src/mongo/shell/assert.js:729:16
DB.prototype._runAggregate@src/mongo/shell/db.js:266:5
DBCollection.prototype.aggregate@src/mongo/shell/collection.js:1058:12
@(shell):1:1

Null Values

Providing null also results in an error.

Suppose we have the following document:

{ "_id" : 8, "data" : null }

The data field contains null.

Let’s apply $strLenCP to that document:

db.other.aggregate(
   [
     { $match: { _id: { $in: [ 8 ] } } },
     {
       $project:
          {
            _id: 0,
            data: 1,
            result: { $strLenCP: "$data" }
          }
     }
   ]
)

Result:

Error: command failed: {
	"ok" : 0,
	"errmsg" : "$strLenCP requires a string argument, found: null",
	"code" : 34471,
	"codeName" : "Location34471"
} : aggregate failed :
_getErrorWithCode@src/mongo/shell/utils.js:25:13
doassert@src/mongo/shell/assert.js:18:14
_assertCommandWorked@src/mongo/shell/assert.js:639:17
assert.commandWorked@src/mongo/shell/assert.js:729:16
DB.prototype._runAggregate@src/mongo/shell/db.js:266:5
DBCollection.prototype.aggregate@src/mongo/shell/collection.js:1058:12
@(shell):1:1

Missing Fields

Continuing with the theme of producing errors, specifying a non-existent field also produces an error.

Document:

{ "_id" : 9 }

Apply $strLenCP:

db.other.aggregate(
   [
     { $match: { _id: { $in: [ 9 ] } } },
     {
       $project:
          {
            _id: 0,
            data: 1,
            result: { $strLenCP: "$data" }
          }
     }
   ]
)

Result:

Error: command failed: {
	"ok" : 0,
	"errmsg" : "$strLenCP requires a string argument, found: missing",
	"code" : 34471,
	"codeName" : "Location34471"
} : aggregate failed :
_getErrorWithCode@src/mongo/shell/utils.js:25:13
doassert@src/mongo/shell/assert.js:18:14
_assertCommandWorked@src/mongo/shell/assert.js:639:17
assert.commandWorked@src/mongo/shell/assert.js:729:16
DB.prototype._runAggregate@src/mongo/shell/db.js:266:5
DBCollection.prototype.aggregate@src/mongo/shell/collection.js:1058:12
@(shell):1:1