Cara menggunakan indexofcp mongodb

A tag already exists with the provided branch name. Many Git commands accept both tag and branch names, so creating this branch may cause unexpected behavior. Are you sure you want to create this branch?

Yes, for this, use $indexOfCP operator along with aggregate framework. Let us first create a collection with documents −

> db.patterDemo.insertOne(
   {
      "ClientName": "John", "ClientWebsiteName":"webbuziness.com/John/business"
   }
);
{
   "acknowledged" : true,
   "insertedId" : ObjectId("5cea40acef71edecf6a1f68d")
}
> db.patterDemo.insertOne(
   {
      "ClientName": "Carol", "ClientWebsiteName":"solvecoding.com/business"
   }
);
{
   "acknowledged" : true,
   "insertedId" : ObjectId("5cea40acef71edecf6a1f68e")
}

Following is the query to display all documents from a collection with the help of find() method −

> db.patterDemo.find().pretty();

This will produce the following output −

{
   "_id" : ObjectId("5cea40acef71edecf6a1f68d"),
   "ClientName" : "John",
   "ClientWebsiteName" : "abcd.com"
}
{
   "_id" : ObjectId("5cea40acef71edecf6a1f68e"),
   "ClientName" : "Carol",
   "ClientWebsiteName" : "pqrs.com"
}

Following is the query to use field value as pattern in $regex −

> db.patterDemo.aggregate([ { "$match": { "$expr": { "$ne": [ { "$indexOfCP": ["$ClientWebsiteName", "$ClientName"] }, -1 ] } }} ]);

This will produce the following output −

{ "_id" : ObjectId("5cea40acef71edecf6a1f68d"), "ClientName" : "John", "ClientWebsiteName" : "abcd.com" }

Cara menggunakan indexofcp mongodb


Cara menggunakan indexofcp mongodb

Terjemahan disediakan oleh mesin penerjemah. Jika konten terjemahan yang diberikan bertentangan dengan versi bahasa Inggris aslinya, utamakan versi bahasa Inggris.

Amazon DocumentDB (dengan kompatibilitas MongoDB) adalah layanan basis data dokumen yang cepat, dapat diskalakan, sangat tersedia, dan terkelola penuh yang mendukung beban kerja MongoDB. Amazon DocumentDB kompatibel dengan API MongoDB 3.6 dan 4.0. Bagian ini mencantumkan fungsionalitas yang didukung. Untuk dukungan menggunakan API dan driver MongoDB, silakan berkonsultasi dengan Forum Komunitas MongoDB. Untuk dukungan menggunakan layanan Amazon DocumentDB, silakan hubungi tim dukungan AWS yang sesuai. Untuk perbedaan fungsional antara Amazon DocumentDB dan MongoDB, silakan lihat Perbedaan Fungsional: Amazon DocumentDB dan MongoDB.

Perintah dan operator MongoDB yang hanya berlaku untuk internal atau tidak berlaku untuk layanan terkelola penuh tidak didukung dan tidak termasuk dalam daftar fungsi yang didukung.

Kami telah menambahkan lebih dari 50+ kemampuan tambahan sejak diluncurkan, dan akan terus bekerja mundur dari pelanggan kami untuk memberikan kemampuan yang mereka butuhkan. Untuk informasi tentang peluncuran terbaru, lihat Pengumuman Amazon DocumentDB.

Jika ada fitur yang tidak didukung yang Anda ingin kami bangun, beri tahu kami dengan mengirimkan email dengan accountID Anda, fitur yang diminta, dan kasus penggunaan ke tim layanan Amazon DocumentDB.

In MongoDB, the

db.test.aggregate(
   [
     { $match: { _id: { $in: [ 1, 2, 3 ] } } },
     {
       $project:
          {
            _id: 0,
            data: 1,
            result: { $indexOfCP: [ "$data", "2021" ] }
          }
     }
   ]
)
4 aggregation pipeline operator searches a string for an occurrence of a substring and returns the UTF code point index of the first occurrence.

The UTF code point index is zero-based (i.e. it starts at

db.test.aggregate(
   [
     { $match: { _id: { $in: [ 1, 2, 3 ] } } },
     {
       $project:
          {
            _id: 0,
            data: 1,
            result: { $indexOfCP: [ "$data", "2021" ] }
          }
     }
   ]
)
5).

Syntax

The syntax goes like this:

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

Where:

  • db.test.aggregate(
       [
         { $match: { _id: { $in: [ 1, 2, 3 ] } } },
         {
           $project:
              {
                _id: 0,
                data: 1,
                result: { $indexOfCP: [ "$data", "2021" ] }
              }
         }
       ]
    )
    6 is the string to search.
  • db.test.aggregate(
       [
         { $match: { _id: { $in: [ 1, 2, 3 ] } } },
         {
           $project:
              {
                _id: 0,
                data: 1,
                result: { $indexOfCP: [ "$data", "2021" ] }
              }
         }
       ]
    )
    7 is the substring you want to find in the string.
  • db.test.aggregate(
       [
         { $match: { _id: { $in: [ 1, 2, 3 ] } } },
         {
           $project:
              {
                _id: 0,
                data: 1,
                result: { $indexOfCP: [ "$data", "2021" ] }
              }
         }
       ]
    )
    8 is an optional argument that specifies a starting index position for the search. Can be any valid expression that resolves to a non-negative integral number.
  • db.test.aggregate(
       [
         { $match: { _id: { $in: [ 1, 2, 3 ] } } },
         {
           $project:
              {
                _id: 0,
                data: 1,
                result: { $indexOfCP: [ "$data", "2021" ] }
              }
         }
       ]
    )
    9 is an optional argument that specifies an ending index position for the search. Can be any valid expression that resolves to a non-negative integral number.

If the specified value isn’t found,

db.test.aggregate(
   [
     { $match: { _id: { $in: [ 1, 2, 3 ] } } },
     {
       $project:
          {
            _id: 0,
            data: 1,
            result: { $indexOfCP: [ "$data", "2021" ] }
          }
     }
   ]
)
4 returns
{ "data" : "c 2021", "result" : 2 }
{ "data" : "© 2021", "result" : 2 }
{ "data" : "ไม้เมือง", "result" : -1 }
1.

If there are multiple instances of the specified value, just the first one is returned.

Example

Suppose we have a collection called

{ "data" : "c 2021", "result" : 2 }
{ "data" : "© 2021", "result" : 2 }
{ "data" : "ไม้เมือง", "result" : -1 }
2 with the following documents:

{ "_id" : 1, "data" : "c 2021" }
{ "_id" : 2, "data" : "© 2021" }
{ "_id" : 3, "data" : "ไม้เมือง" }

Here’s an example of applying

db.test.aggregate(
   [
     { $match: { _id: { $in: [ 1, 2, 3 ] } } },
     {
       $project:
          {
            _id: 0,
            data: 1,
            result: { $indexOfCP: [ "$data", "2021" ] }
          }
     }
   ]
)
4 to those documents:

db.test.aggregate(
   [
     { $match: { _id: { $in: [ 1, 2, 3 ] } } },
     {
       $project:
          {
            _id: 0,
            data: 1,
            result: { $indexOfCP: [ "$data", "2021" ] }
          }
     }
   ]
)

Result:

{ "data" : "c 2021", "result" : 2 }
{ "data" : "© 2021", "result" : 2 }
{ "data" : "ไม้เมือง", "result" : -1 }

In the first two documents, the substring was found at UTF code point index position

{ "data" : "c 2021", "result" : 2 }
{ "data" : "© 2021", "result" : 2 }
{ "data" : "ไม้เมือง", "result" : -1 }
4. Given
db.test.aggregate(
   [
     { $match: { _id: { $in: [ 1, 2, 3 ] } } },
     {
       $project:
          {
            _id: 0,
            data: 1,
            result: { $indexOfCP: [ "$data", "2021" ] }
          }
     }
   ]
)
4 results are zero-based (the index starts at
db.test.aggregate(
   [
     { $match: { _id: { $in: [ 1, 2, 3 ] } } },
     {
       $project:
          {
            _id: 0,
            data: 1,
            result: { $indexOfCP: [ "$data", "2021" ] }
          }
     }
   ]
)
5) the position 2 represents the third code point.

This is a different result to what we’d get if we use

{ "data" : "c 2021", "result" : 2 }
{ "data" : "© 2021", "result" : 2 }
{ "data" : "ไม้เมือง", "result" : -1 }
7, because the copyright symbol (
{ "data" : "c 2021", "result" : 2 }
{ "data" : "© 2021", "result" : 2 }
{ "data" : "ไม้เมือง", "result" : -1 }
8) in the second document takes up 2 bytes. But it uses just one code point, which is the same as what the letter
{ "data" : "c 2021", "result" : 2 }
{ "data" : "© 2021", "result" : 2 }
{ "data" : "ไม้เมือง", "result" : -1 }
9 uses.

Regarding the third document, the substring wasn’t found at all, and so the result is

{ "data" : "c 2021", "result" : 2 }
{ "data" : "© 2021", "result" : 2 }
{ "data" : "ไม้เมือง", "result" : -1 }
1.

Here’s another example, except this time we search for a Thai character:

db.test.aggregate(
   [
     { $match: { _id: { $in: [ 1, 2, 3 ] } } },
     {
       $project:
          {
            _id: 0,
            data: 1,
            result: { $indexOfCP: [ "$data", "เ" ] }
          }
     }
   ]
)

Result:

{ "data" : "c 2021", "result" : -1 }
{ "data" : "© 2021", "result" : -1 }
{ "data" : "ไม้เมือง", "result" : 3 }

In this case, we searched for a character that’s in the third document, and its UTF-8 code point index comes back as

db.test.aggregate(
   [
     { $match: { _id: { $in: [ 1, 2, 3 ] } } },
     {
       $project:
          {
            _id: 0,
            data: 1,
            result: { $indexOfCP: [ "$data", "เ" ] }
          }
     }
   ]
)
1. Given
db.test.aggregate(
   [
     { $match: { _id: { $in: [ 1, 2, 3 ] } } },
     {
       $project:
          {
            _id: 0,
            data: 1,
            result: { $indexOfCP: [ "$data", "2021" ] }
          }
     }
   ]
)
4 results are zero based, this means it’s the fourth code point.

This is because the second character has a diacritic mark, which is also a code point. Therefore, the first character is one code point and the second character is two code points (including the diacritic), which equals three. This means that our character starts at the fourth position (which is code point number

db.test.aggregate(
   [
     { $match: { _id: { $in: [ 1, 2, 3 ] } } },
     {
       $project:
          {
            _id: 0,
            data: 1,
            result: { $indexOfCP: [ "$data", "เ" ] }
          }
     }
   ]
)
1, due to index count starting at
db.test.aggregate(
   [
     { $match: { _id: { $in: [ 1, 2, 3 ] } } },
     {
       $project:
          {
            _id: 0,
            data: 1,
            result: { $indexOfCP: [ "$data", "2021" ] }
          }
     }
   ]
)
5).

See MongoDB

db.test.aggregate(
   [
     { $match: { _id: { $in: [ 1, 2, 3 ] } } },
     {
       $project:
          {
            _id: 0,
            data: 1,
            result: { $indexOfCP: [ "$data", "เ" ] }
          }
     }
   ]
)
5 for an example that returns the number of code points for each character in this particular string. And see MongoDB
db.test.aggregate(
   [
     { $match: { _id: { $in: [ 1, 2, 3 ] } } },
     {
       $project:
          {
            _id: 0,
            data: 1,
            result: { $indexOfCP: [ "$data", "เ" ] }
          }
     }
   ]
)
6 to see the number of bytes in the same string.

Specify a Starting Position

You can provide a third argument to specify a starting index position for the search.

Suppose we have the following document:

{ "_id" : 4, "data" : "ABC XYZ ABC" }

Here’s an example of applying

db.test.aggregate(
   [
     { $match: { _id: { $in: [ 1, 2, 3 ] } } },
     {
       $project:
          {
            _id: 0,
            data: 1,
            result: { $indexOfCP: [ "$data", "2021" ] }
          }
     }
   ]
)
4 with a starting position:

db.test.aggregate(
   [
     { $match: { _id: { $in: [ 4 ] } } },
     {
       $project:
          {
            _id: 0,
            data: 1,
            result: { $indexOfCP: [ "$data", "ABC", 1 ] }
          }
     }
   ]
)

Result:

{ "data" : "ABC XYZ ABC", "result" : 8 }

In this case, the second instance of the substring was returned. This is because we started the search at position

db.test.aggregate(
   [
     { $match: { _id: { $in: [ 1, 2, 3 ] } } },
     {
       $project:
          {
            _id: 0,
            data: 1,
            result: { $indexOfCP: [ "$data", "เ" ] }
          }
     }
   ]
)
8, and the first instance of the substring starts at position
db.test.aggregate(
   [
     { $match: { _id: { $in: [ 1, 2, 3 ] } } },
     {
       $project:
          {
            _id: 0,
            data: 1,
            result: { $indexOfCP: [ "$data", "2021" ] }
          }
     }
   ]
)
5 (before the starting position for the search).

If the start position is a number greater than the string or greater than the ending position,

db.test.aggregate(
   [
     { $match: { _id: { $in: [ 1, 2, 3 ] } } },
     {
       $project:
          {
            _id: 0,
            data: 1,
            result: { $indexOfCP: [ "$data", "2021" ] }
          }
     }
   ]
)
4 returns
{ "data" : "c 2021", "result" : 2 }
{ "data" : "© 2021", "result" : 2 }
{ "data" : "ไม้เมือง", "result" : -1 }
1.

If it’s a negative number,

db.test.aggregate(
   [
     { $match: { _id: { $in: [ 1, 2, 3 ] } } },
     {
       $project:
          {
            _id: 0,
            data: 1,
            result: { $indexOfCP: [ "$data", "2021" ] }
          }
     }
   ]
)
4 returns an error.

Specify an Ending Position

You can also provide a fourth argument to specify the ending index position for the search.

If you provide this argument, you also need to provide a starting position. Failing to do so will result in this argument being interpreted as the starting point.

Example:

db.test.aggregate(
   [
     { $match: { _id: { $in: [ 4 ] } } },
     {
       $project:
          {
            _id: 0,
            data: 1,
            result: { $indexOfCP: [ "$data", "XYZ", 0, 3 ] }
          }
     }
   ]
)

Result:

{ "_id" : 1, "data" : "c 2021" }
{ "_id" : 2, "data" : "© 2021" }
{ "_id" : 3, "data" : "ไม้เมือง" }
0

The result is

{ "data" : "c 2021", "result" : 2 }
{ "data" : "© 2021", "result" : 2 }
{ "data" : "ไม้เมือง", "result" : -1 }
1 which means the substring wasn’t found. That’s because we started our search at position
db.test.aggregate(
   [
     { $match: { _id: { $in: [ 1, 2, 3 ] } } },
     {
       $project:
          {
            _id: 0,
            data: 1,
            result: { $indexOfCP: [ "$data", "2021" ] }
          }
     }
   ]
)
5 and ended it at position
db.test.aggregate(
   [
     { $match: { _id: { $in: [ 1, 2, 3 ] } } },
     {
       $project:
          {
            _id: 0,
            data: 1,
            result: { $indexOfCP: [ "$data", "เ" ] }
          }
     }
   ]
)
1, therefore not capturing the substring.

Here’s what happens if we increment the ending index position:

{ "_id" : 1, "data" : "c 2021" }
{ "_id" : 2, "data" : "© 2021" }
{ "_id" : 3, "data" : "ไม้เมือง" }
1

Result:

{ "_id" : 1, "data" : "c 2021" }
{ "_id" : 2, "data" : "© 2021" }
{ "_id" : 3, "data" : "ไม้เมือง" }
2

This time the value was included and its index position returned.

If the end position is a number less than the starting position,

db.test.aggregate(
   [
     { $match: { _id: { $in: [ 1, 2, 3 ] } } },
     {
       $project:
          {
            _id: 0,
            data: 1,
            result: { $indexOfCP: [ "$data", "2021" ] }
          }
     }
   ]
)
4 returns
{ "data" : "c 2021", "result" : 2 }
{ "data" : "© 2021", "result" : 2 }
{ "data" : "ไม้เมือง", "result" : -1 }
1.

If it’s a negative number,

db.test.aggregate(
   [
     { $match: { _id: { $in: [ 1, 2, 3 ] } } },
     {
       $project:
          {
            _id: 0,
            data: 1,
            result: { $indexOfCP: [ "$data", "2021" ] }
          }
     }
   ]
)
4 returns an error.

Missing Fields

If the field is not in the document,

db.test.aggregate(
   [
     { $match: { _id: { $in: [ 1, 2, 3 ] } } },
     {
       $project:
          {
            _id: 0,
            data: 1,
            result: { $indexOfCP: [ "$data", "2021" ] }
          }
     }
   ]
)
4 returns
{ "_id" : 4, "data" : "ABC XYZ ABC" }
0.

Suppose we have the following document:

{ "_id" : 1, "data" : "c 2021" }
{ "_id" : 2, "data" : "© 2021" }
{ "_id" : 3, "data" : "ไม้เมือง" }
3

Here’s what happens when we apply

db.test.aggregate(
   [
     { $match: { _id: { $in: [ 1, 2, 3 ] } } },
     {
       $project:
          {
            _id: 0,
            data: 1,
            result: { $indexOfCP: [ "$data", "2021" ] }
          }
     }
   ]
)
4:

{ "_id" : 1, "data" : "c 2021" }
{ "_id" : 2, "data" : "© 2021" }
{ "_id" : 3, "data" : "ไม้เมือง" }
4

Result:

{ "_id" : 1, "data" : "c 2021" }
{ "_id" : 2, "data" : "© 2021" }
{ "_id" : 3, "data" : "ไม้เมือง" }
5

Null Values

If the first argument is

{ "_id" : 4, "data" : "ABC XYZ ABC" }
0,
db.test.aggregate(
   [
     { $match: { _id: { $in: [ 1, 2, 3 ] } } },
     {
       $project:
          {
            _id: 0,
            data: 1,
            result: { $indexOfCP: [ "$data", "2021" ] }
          }
     }
   ]
)
4 returns
{ "_id" : 4, "data" : "ABC XYZ ABC" }
0.

Suppose we have the following document:

{ "_id" : 1, "data" : "c 2021" }
{ "_id" : 2, "data" : "© 2021" }
{ "_id" : 3, "data" : "ไม้เมือง" }
6

Here’s what happens when we apply

db.test.aggregate(
   [
     { $match: { _id: { $in: [ 1, 2, 3 ] } } },
     {
       $project:
          {
            _id: 0,
            data: 1,
            result: { $indexOfCP: [ "$data", "2021" ] }
          }
     }
   ]
)
4:

{ "_id" : 1, "data" : "c 2021" }
{ "_id" : 2, "data" : "© 2021" }
{ "_id" : 3, "data" : "ไม้เมือง" }
7

Result:

{ "_id" : 1, "data" : "c 2021" }
{ "_id" : 2, "data" : "© 2021" }
{ "_id" : 3, "data" : "ไม้เมือง" }
8

However, when the second argument (i.e. the substring) is

{ "_id" : 4, "data" : "ABC XYZ ABC" }
0, an error is returned:

{ "_id" : 1, "data" : "c 2021" }
{ "_id" : 2, "data" : "© 2021" }
{ "_id" : 3, "data" : "ไม้เมือง" }
9

Result:

db.test.aggregate(
   [
     { $match: { _id: { $in: [ 1, 2, 3 ] } } },
     {
       $project:
          {
            _id: 0,
            data: 1,
            result: { $indexOfCP: [ "$data", "2021" ] }
          }
     }
   ]
)
0

Wrong Data Type

If the first argument is the wrong data type (i.e. it doesn’t resolve to a string),

db.test.aggregate(
   [
     { $match: { _id: { $in: [ 1, 2, 3 ] } } },
     {
       $project:
          {
            _id: 0,
            data: 1,
            result: { $indexOfCP: [ "$data", "2021" ] }
          }
     }
   ]
)
4 returns an error.