For AI agents: a documentation index is available at https://www.mongodb.com/docs/llms.txt — markdown versions of all pages are available by appending .md to any URL path.
Docs Menu

$substr (expression operator)

$substr

Deprecated since version 3.4: $substr is 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.

$substr has 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.

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.

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'
}
]
Rate this page