Definition
$substrDeprecated since version 3.4:
$substris now an alias for$substrBytes.Returns a substring of a string, starting at a specified index position and including the specified number of characters. The index is zero-based.
$substrhas the following syntax:{ $substr: [ <string>, <start>, <length> ] } The arguments can be any valid expression as long as the first argument resolves to a string, and the second and third arguments resolve to integers. For more information on expressions, see Expressions.
Behavior
If <start> is a negative number, $substr returns an empty string "".
If <length> is a negative number, $substr returns a substring that starts at the specified index and includes the rest of the string.
$substr only has a well-defined behavior for strings of ASCII characters.
Example
The following example uses the movies collection from the sample_mflix
sample dataset. For details on how to load this dataset into your deployment, see Load the sample dataset.
This aggregation operation uses the $substr operator to separate the title value into a titleStart and a titleRest. The second expression uses a negative <length> to return the characters from index 3 to the end of the string:
db.movies.aggregate( [ { $match: { runtime: { $gt: 1000 } } }, { $project: { _id: 0, title: 1, titleStart: { $substr: [ "$title", 0, 3 ] }, titleRest: { $substr: [ "$title", 3, -1 ] } } } ] )
[ { title: 'Centennial', titleStart: 'Cen', titleRest: 'tennial' }, { title: 'Baseball', titleStart: 'Bas', titleRest: 'eball' } ]