快速入门
安装
基本操作
数据模型
SQL引擎
系统架构
数据库管理
连接器
驱动
参考手册
故障排除
SAC
版本信息
{ <字段名>: { $substr: <值> } }
返回字符串的子串。原始值为数组类型时对每个数组元素执行该操作,非字符串类型返回 null 。
find({},{<fieldName>:{<$substr:[Pos,Len]>}})
find({},{<fieldName>:{<$substr:Value>}})
在集合 foo.bar 插入1条记录:
> db.foo.bar.insert( { "a": "abcdefg" } )
SequoiaDB shell 运行如下:
作为选择符使用:
返回下标为0,长度为2的子串:
> db.foo.bar.find( {}, { "a": { "$substr": 2 } } ) { "_id": { "$oid": "58257afbec5c9b3b7e000002" }, "a": "ab" } Return 1 row(s).
返回倒数第二个字符开始的子串:
> db.foo.bar.find( {}, { "a": { "$substr": -2 } } ) { "_id": { "$oid": "58257afbec5c9b3b7e000002" }, "a": "fg" } Return 1 row(s).
返回下标为2,长度为3的子串:
> db.foo.bar.find( {}, { "a": { "$substr": [ 2, 3 ] } } ) { "_id": { "$oid": "58257afbec5c9b3b7e000002" }, "a": "cde" } Return 1 row(s).
返回倒数第二个字符开始,长度为3的子串:
> db.foo.bar.find( {}, { "a": { "$substr": [ -2, 3 ] } } ) { "_id": { "$oid": "58257afbec5c9b3b7e000002" }, "a": "fg" } Return 1 row(s).
Note:
子串长度不足3。
与匹配符配合使用:
匹配字段“a”下标为2,长度为3的子串为“cde”的记录:
> db.foo.bar.find( { "a": { "$substr": [ 2, 3 ], "$et": "cde" } } ) { "_id": { "$oid": "58257afbec5c9b3b7e000002" }, "a": "abcdefg" } Return 1 row(s).