$indexOfCP
The $indexOfCP operator in Amazon DocumentDB is used to find the index, in code points (CP), of the first occurrence of a specified substring within a string expression. This can be useful when parsing and extracting content from string fields.
Parameters
-
string expression: The string to search. -
substring: The substring to search for. -
[<start>]: (optional) The position to start the search (zero-based index). Default is 0.
Example (MongoDB Shell)
In this example, we use the $indexOfCP operator to find the index of the first occurrence of the hyphen character - in the Desk field of each document.
Create sample documents
db.people.insertMany([ { "_id":1, "name":"John Doe", "Manager":"Jane Doe", "Role":"Developer", "Desk": "Düsseldorf-BVV-021"}, { "_id":2, "name":"John Stiles", "Manager":"Jane Doe", "Role":"Manager", "Desk": "Munich-HGG-32a"}, { "_id":3, "name":"Richard Roe", "Manager":"Jorge Souza", "Role":"Product", "Desk": "Cologne-ayu-892.50"}, { "_id":4, "name":"Mary Major", "Manager":"Jane Doe", "Role":"Solution Architect", "Desk": "Dortmund-Hop-78"} ])
Query example
db.people.aggregate([ { $project: { stateLocation: { $indexOfCP: [ "$Desk", "-"] } } } ])
Output
{ "_id" : 1, "stateLocation" : 10 }
{ "_id" : 2, "stateLocation" : 6 }
{ "_id" : 3, "stateLocation" : 7 }
{ "_id" : 4, "stateLocation" : 8 }
Code examples
To view a code example for using the $indexOfCP command, choose the tab for the language that you want to use: