$substr

    1. find({},{<fieldName>:{<$substr:[Pos,Len]>}})
    • Pos 代表从字符串返回子串的起始下标,当Pos为负数时代表从字符串末尾倒数的位置。例如-2代表从字符串倒数第二个字符开始截取子串。
    • Len 代表截取的长度。
    1. find({},{<fieldName>:{<$substr:Value>}})
    • 当 Value 大于等于0时,表示返回从下标 0 开始,长度为Value的子串。
    • 当 Value 小于0时,代表返回从下标 Value 开始的子串,负数代表从字符串末尾倒数的位置。例如-2代表从字符串倒数第二个字符开始截取子串。

    在集合 foo.bar 插入1条记录:

    SequoiaDB shell 运行如下:

    • 作为选择符使用:
    1. > db.foo.bar.find( {}, { "a": { "$substr": 2 } } )
    2. {
    3. "_id": {
    4. },
    5. "a": "ab"
    6. }
    7. Return 1 row(s).

    返回倒数第二个字符开始的子串:

    1. > db.foo.bar.find( {}, { "a": { "$substr": -2 } } )
    2. {
    3. "_id": {
    4. "$oid": "58257afbec5c9b3b7e000002"
    5. },
    6. "a": "fg"
    7. }
    8. Return 1 row(s).

    返回下标为2,长度为3的子串:

    1. > db.foo.bar.find( {}, { "a": { "$substr": [ -2, 3 ] } } )
    2. {
    3. "$oid": "58257afbec5c9b3b7e000002"
    4. },
    5. "a": "fg"
    6. }
    7. Return 1 row(s).
    • 与匹配符配合使用:

    匹配字段“a”下标为2,长度为3的子串为“cde”的记录:

    1. > db.foo.bar.find( { "a": { "$substr": [ 2, 3 ], "$et": "cde" } } )
    2. {
    3. "_id": {
    4. "$oid": "58257afbec5c9b3b7e000002"
    5. },
    6. "a": "abcdefg"
    7. }
    8. Return 1 row(s).