Introduction to MongoDB $indexOfBytes Operator

$indexOfBytes is a string operator in MongoDB used to find the first occurrence of a substring in a string and return its starting position.

Syntax

The syntax of the $indexOfBytes operator is as follows:

{ $indexOfBytes: [ <string>, <substring>, <start>, <end> ] }

Here, <string> and <substring> must both be of string type, representing the string to search and the substring to search for, respectively. <start> and <end> are optional parameters representing the range to search, and must both be of integer type, with <end> being greater than or equal to <start>.

Use Cases

The $indexOfBytes operator is commonly used for the following scenarios:

  • Searching for a specified substring in a string;
  • Checking if a substring is contained in a string.

Example

Here is an example of using the $indexOfBytes operator to find a character index in a string. Suppose we have the following data:

{ "name": "John Doe" }
{ "name": "Jane Doe" }
{ "name": "Bob Smith" }

Now, we want to query for records that contain the character “D”. In the MongoDB Shell, we can use the $indexOfBytes operator to query as follows:

> db.collection.find({ name: { $indexOfBytes: { $substr: ["$name", 0, 1] } } >= 0 })
{ "name": "John Doe" }
{ "name": "Jane Doe" }

In this example, we use the $substr operator to extract the first character of each record, and use the $indexOfBytes operator to find the index of the character “D” in the extracted character. If the index is greater than or equal to 0, it means that the record contains the character “D”, and that record is returned.

Conclusion

The $indexOfBytes operator can conveniently find a specified substring in a string and return the position where the substring first appears. In practical applications, we can use the $indexOfBytes operator to check if a substring is contained in a string or obtain the position information of the substring.