$substr
- find({},{<fieldName>:{<$substr:[Pos,Len]>}})
- Pos 代表从字符串返回子串的起始下标,当Pos为负数时代表从字符串末尾倒数的位置。例如-2代表从字符串倒数第二个字符开始截取子串。
- Len 代表截取的长度。
- find({},{<fieldName>:{<$substr:Value>}})
- 当 Value 大于等于0时,表示返回从下标 0 开始,长度为Value的子串。
- 当 Value 小于0时,代表返回从下标 Value 开始的子串,负数代表从字符串末尾倒数的位置。例如-2代表从字符串倒数第二个字符开始截取子串。
在集合 foo.bar 插入1条记录:
SequoiaDB shell 运行如下:
- 作为选择符使用:
- > db.foo.bar.find( {}, { "a": { "$substr": 2 } } )
- {
- "_id": {
- },
- "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 ] } } )
- {
- "$oid": "58257afbec5c9b3b7e000002"
- },
- "a": "fg"
- }
- Return 1 row(s).
- 与匹配符配合使用:
匹配字段“a”下标为2,长度为3的子串为“cde”的记录:
- > db.foo.bar.find( { "a": { "$substr": [ 2, 3 ], "$et": "cde" } } )
- {
- "_id": {
- "$oid": "58257afbec5c9b3b7e000002"
- },
- "a": "abcdefg"
- }
- Return 1 row(s).