$indexOfCP - Amazon DocumentDB
Services or capabilities described in Amazon Web Services documentation might vary by Region. To see the differences applicable to the China Regions, see Getting Started with Amazon Web Services in China (PDF).

$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:

Node.js
const { MongoClient } = require('mongodb'); async function main() { const client = await MongoClient.connect('mongodb://<username>:<password>@<cluster-endpoint>:27017/?tls=true&tlsCAFile=global-bundle.pem&replicaSet=rs0&readPreference=secondaryPreferred&retryWrites=false'); const db = client.db('test'); const collection = db.collection('people'); const result = await collection.aggregate([ { $project: { stateLocation: { $indexOfCP: [ "$Desk", "-"] } } } ]).toArray(); console.log(result); await client.close(); } main();
Python
from pymongo import MongoClient client = MongoClient('mongodb://<username>:<password>@<cluster-endpoint>:27017/?tls=true&tlsCAFile=global-bundle.pem&replicaSet=rs0&readPreference=secondaryPreferred&retryWrites=false') db = client['test'] collection = db['people'] result = list(collection.aggregate([ { '$project': { 'stateLocation': { '$indexOfCP': [ '$Desk', '-' ] } } } ])) print(result) client.close()