Welcome to plsql4all.blogspot.com SQL, MYSQL, ORACLE, TERADATA, MONGODB, MARIADB, GREENPLUM, DB2, POSTGRESQL.

Friday, 23 February 2024

position Function in MongoDB

In MongoDB, the equivalent function to find the position of a substring within a string is the $indexOfCP aggregation operator. This operator is used within aggregation pipelines to return the position of a substring within a string.


Here's the syntax of the $indexOfCP operator in MongoDB:


{ $indexOfCP: [ <string>, <substring> ] }


- <string>: The string in which to search for the substring.

- <substring>: The substring to search for within the string.


Example:

db.collection.aggregate([

  {

    $project: {

      position: { $indexOfCP: ["hello world", "world"] }

    }

  }

])

This will return documents with the position field indicating the position of the substring 'world' within the string 'hello world'. In this case, it will return 6, indicating that the substring 'world' starts at the 6th position within the string.

No comments:

Post a Comment

Please provide your feedback in the comments section above. Please don't forget to follow.