Open In App

MongoDB $substrCP Operator

Last Updated : 01 May, 2024
Improve
Improve
Like Article
Like
Save
Share
Report

MongoDB $substrCP Operator extracts a substring from the given string expression.

$substrCP Operator in MongoDB

The $substrCP Operator in MongoDB is used in the aggregation pipeline to find substrings from a given string expression.

It uses the Unicode code point index and count to determine the substring. This makes it useful when working with strings that contain non-ASCII characters, as it handles the Unicode code points correctly.

For example: { $substrCP: [ “geeksforgeeks”, 0, 5 ] } will give the output as “geeks” as 0 is given as starting location, and from there 5 characters need to be taken and hence “geeks” is the result

Syntax

{ $substrCP: [ <your string expression>, <code point index>, <code point count> ] }

Parameters: 

  • your string expression: It is a valid string expression with alpha/alphanumeric and also with special characters from which the substring will be extracted.
  • code point index: It is a number(non-negative integer) that represents the starting point of the substring
  • code point counts: Non-negative integer(number) and it specifies the number of characters that need to be taken from the code point index.

MongoDB $substrCP Operator Examples

To understand it better, let’s look at some examples of the MongoDB $substrCP operator.

In the following examples, we are working with:

Database: geeksforgeeks
Collection: articles
Documents: three documents that contain the details of the articles in the form of field-value pairs.

demo database and collection

Using $substrCP operator Example

Now, displaying the above article’s output by splitting the “publishedon” column details as “publicationmonth” and “publicationyear” by using the below query:

db.articles.aggregate(

[

{

$project: {

articlename: 1,

publicationmonth: { $substrCP: [ "$publishedon", 0, 4 ] },

publicationyear: {

$substrCP: [

"$publishedon", 4, { $subtract: [ { $strLenCP: "$publishedon" }, 4 ] }

]

}

}

}

]

)

Here, “publicationmonth” is following straight syntax and it picks the first 4 characters of “publishedon” column. In “publicationyear”, rest of the characters of “publishedon” are taken. It uses “$subtract” which is used for subtracting the byte index from the length of the string by using $strLenCP.

Output:

Important Points About MongoDB $substrCP Operator

  1. The $substrCP operator is used in the aggregation pipeline to extract a substring from a given string expression.
  2. It uses the Unicode code point index and count to determine the substring.
  3. The $substrCP operator is useful when working with strings that contain non-ASCII characters, as it handles the Unicode code points correctly.
  4. The $substrCP operator is designed to work efficiently within the MongoDB aggregation framework, providing a way to manipulate string data based on Unicode code points.

Like Article
Suggest improvement
Previous
Next
Share your thoughts in the comments

Similar Reads