db.command.aggregate.strLenCP
1. Operator Description
Function: Calculates and returns the number of UTF-8 code points in the specified string.
Declaration: db.command.aggregate.strLenCP(<expression>)
2. Operator Parameters
Field | Type | Required | Description |
---|---|---|---|
- | Expression | Required | aggregate expression that can be resolved to a string |
3. Sample Code
Suppose the collection students
contains the following records:
{ "name": "dongyuanxin", "nickname": "Xin Tan" }
Use strLenCP
to calculate the number of UTF-8 code points in the values of the name
and nickname
fields:
// Sample code in the Cloud Function environment
const tcb = require('@cloudbase/node-sdk')
const app = tcb.init({
env: 'xxx'
})
const db = app.database()
const $ = db.command.aggregate
const _ = db.command
exports.main = async (event, context) => {
const res = await db
.collection('students')
.aggregate()
.project({
_id: 0,
nameLength: $.strLenCP('$name'),
nicknameLength: $.strLenCP('$nickname')
})
.end()
console.log(res.data)
}
The returned result is as follows:
{ "nameLength": 11, "nicknameLength": 2 }