$substr,$substrBytes,$substrCP
是aggregate的管道操作符,主要可用在project中,
$substr
在版本3.4后最好使用$substrBytes
substr
db.getCollection("table_name").aggregate({
$project: {
"new_string": {
$substr: [< string > , < start > , < length > ]
}
}
})
其中< string >是需截取的字符串,若为表内字段可用$加字段名。
< start >截取开始的位置,为整数,整数从0开始。若数字为负数或大于< string >的长度,则返回空字符串""。
< length >截取字符串长度,为整数。若数字为负数则返回< start >后的全部的字符串。
substrBytes
db.getCollection("table_name").aggregate({
$project: {
"new_string": {
$substrBytes : [< string expression >,< byte index >,< byte count >]
}
}
})
其中< string expression >是需截取的字符串,若为表内字段可用$加字段名。
< byte index >截取开始的位置,为整数,整数从0开始。若数字为负数或大于< string >的长度,则返回空字符串""。
< byte count >截取字符串长度,为整数。若数字为负数则返回< start >后的全部的字符串。
substrCP
db.getCollection("table_name").aggregate({
$project: {
"new_string": {
$substrCP:[< string expression >,< code point index >,< code point count >]
}
}
})
其中< string expression >是需截取的字符串,若为表内字段可用$加字段名。
< code point index >截取开始的位置,为整数,整数从0开始。若数字为负数或大于< string >的长度,则返回空字符串""。
< code point count >截取字符串长度,为整数。若数字为负数则返回< start >后的全部的字符串。
示例:
1.截取字符串
$substr
与$substrBytes
结果相同,会将汉字的字符长度视为2。
$substrCP
将汉字的字符长度视为1。

2.截取字段内容

网友评论