Open In App

$substrCP (aggregation) operator in MongoDB

MongoDB is a NOSQL database and it is frequently getting used across in all sets of industries. The popularity of MongoDB lies in the effective way of querying, fast retrieval of data which helps to have broader insights through analytics from big data. Now we will see $substrCP (aggregation) operator and how effectively it can be used in the project.

$substrCP operator: It is used to find the substrings from a given string. It uses the code point(Unicode codespace) to extract the substring.



Syntax :

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


Parameters: 



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

Example 1:

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.

 Using $substrCP operator finding publicationmonth and publicationyear of articles:

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.

Example 2 :

In the following examples, we are working with:

Database: geeksforgeeks

Collection: articles

Documents: three documents that contain the details of the authors in the form of field-value pairs.

Using $substrCP operator finding the firstname and lastname of the authors:

Now, displaying above authors output by splitting the “name” column details as “firstname” and “lastname” by using the below query:

db.authors.aggregate(

 [

   {

     $project: {

       articlename: 1,

       firstname: { $substrCP: [ "$name", 0, 5 ] },

       lastname: {

         $substrCP: [

           "$name", 5, { $subtract: [ { $strLenCP: "$name" }, 3 ] }

         ]

       }

     }

   }

 ]

)


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

 

Article Tags :