[CSHARP-1661] Add $findChar or $indexOf operator for strings to find position of specific character (or substring) Created: 10/May/16  Updated: 27/May/22  Resolved: 29/Aug/16

Status: Closed
Project: C# Driver
Component/s: Linq
Affects Version/s: None
Fix Version/s: 2.4

Type: Improvement Priority: Major - P3
Reporter: Rathi Gnanasekaran Assignee: Robert Stam
Resolution: Done Votes: 0
Labels: None
Remaining Estimate: Not Specified
Time Spent: Not Specified
Original Estimate: Not Specified

Issue Links:
Depends
depends on SERVER-8951 Add $findChar or $indexOf operator fo... Closed
Epic Link: MongoDB 3.4
Server Compat: 3.3

 Description   

Syntax

{$indexOfBytes: [<string>, <search value>, <start index - optional>, <end index - optional>]}
{$indexOfCP: [<string>, <search value>, <start index - optional>, <end index - optional>]}
{$indexOfArray: [<array>, <search value>, <start index - optional>, <end index - optional>]}

Examples

> db.coll.insert([
  {_id: 1, string: "hello world"}
]);
> db.coll.aggregate([{
  $project: {
    location: {$indexOfBytes: ["$string", "world"]}
  }
}]);
{_id: 1, location: 6}
 
// Example 2 - differentiating code points vs. bytes.
> db.coll.insert([
  {_id: 1, string: "øle"}
]);
> db.coll.aggregate([{
  $project: {
    byteLocation: {$indexOfBytes: ["$string", "le"]},
    cpLocation: {$indexOfCP: ["$string", "le"]}
  }
}]);
{_id: 1, byteLocation: 2, cpLocation: 1}
 
// Example 3 - using the start index.
> db.coll.insert([
  {_id: 1, string: "PREFIX|text with word FIX"},  // Contains "FIX", Should match.
  {_id: 2, string: "PREFIX|text without target"}  // Should not match.
]);
> db.coll.aggregate([{
  $project: {
    containsFix: {$indexOfCP: ["$string", "fix", {$strLenCP: "PREFIX|"}]}
  }
}]);
{_id: 1, containsFix: 22}
{_id: 2, containsFix: -1}
 
 
// Example 4 - using the start and end indices.
> db.coll.insert([
  {_id: 1, string: "PREFIX|text with word FIX|SUFFIX"},  // Contains "FIX", Should match.
  {_id: 2, string: "PREFIX|text without target|SUFFIX"}  // Should not match.
]);
> db.coll.aggregate([{
  $project: {
    containsFix: {
      $let: {
        vars: {
          startIndex: {$strLenCP: "PREFIX|"},  // 7
          endIndex: {$subtract: [0, {$strLenCP: "|SUFFIX"}]}  // -7
        },
        in: {$indexOfCP: ["$string", "fix", "$$startIndex", "$$endIndex"]}
      }
    }
  }
}]);
{_id: 1, containsFix: 22}
{_id: 2, containsFix: -1}

Notes

  • Same functionality as Python's find(). Returns -1 if there were no occurrences.

Errors

  • For indexOfBytes/indexOfCP, if the first two arguments are not strings. For indexOfArray, if the first argument is not an array.
  • If either of the last two arguments are not integral.

Old Description
Would like to have some operator to find character (or substring) in string. Use case - normalizing strings like server names "server1.foo.bar" to short names via $substr operator (without knowing location of first '.')



 Comments   
Comment by Githook User [ 29/Sep/16 ]

Author:

{u'username': u'craiggwilson', u'name': u'Craig Wilson', u'email': u'craiggwilson@gmail.com'}

Message: CSHARP-1661: added bytes and cp variants.
Branch: master
https://github.com/mongodb/mongo-csharp-driver/commit/d04885e8d7b0b84c1cd3eae8960794ac57c8b50f

Comment by Githook User [ 29/Sep/16 ]

Author:

{u'username': u'rstam', u'name': u'rstam', u'email': u'robert@robertstam.org'}

Message: CSHARP-1661: Refactoring.
Branch: master
https://github.com/mongodb/mongo-csharp-driver/commit/9819e4631a9b0b6d7e3bcc79b0fef3655e7056e2

Comment by Githook User [ 29/Sep/16 ]

Author:

{u'username': u'craiggwilson', u'name': u'Craig Wilson', u'email': u'craiggwilson@gmail.com'}

Message: CSHARP-1661: support for $indexOfXXX.
Branch: master
https://github.com/mongodb/mongo-csharp-driver/commit/34f91e62a705d229ea180d0c6d32f72cf35c65a8

Comment by Githook User [ 24/Sep/16 ]

Author:

{u'username': u'craiggwilson', u'name': u'Craig Wilson', u'email': u'craiggwilson@gmail.com'}

Message: CSHARP-1661: added bytes and cp variants.
Branch: v2.4.x
https://github.com/mongodb/mongo-csharp-driver/commit/d04885e8d7b0b84c1cd3eae8960794ac57c8b50f

Comment by Githook User [ 24/Sep/16 ]

Author:

{u'username': u'rstam', u'name': u'rstam', u'email': u'robert@robertstam.org'}

Message: CSHARP-1661: Refactoring.
Branch: v2.4.x
https://github.com/mongodb/mongo-csharp-driver/commit/9819e4631a9b0b6d7e3bcc79b0fef3655e7056e2

Comment by Githook User [ 24/Sep/16 ]

Author:

{u'username': u'craiggwilson', u'name': u'Craig Wilson', u'email': u'craiggwilson@gmail.com'}

Message: CSHARP-1661: support for $indexOfXXX.
Branch: v2.4.x
https://github.com/mongodb/mongo-csharp-driver/commit/34f91e62a705d229ea180d0c6d32f72cf35c65a8

Comment by Githook User [ 21/Sep/16 ]

Author:

{u'username': u'craiggwilson', u'name': u'Craig Wilson', u'email': u'craiggwilson@gmail.com'}

Message: CSHARP-1661: added bytes and cp variants.
Branch: v2.4.x
https://github.com/mongodb/mongo-csharp-driver/commit/4850fd2310581682020484f392c60360fe2d43a4

Comment by Githook User [ 21/Sep/16 ]

Author:

{u'username': u'rstam', u'name': u'rstam', u'email': u'robert@robertstam.org'}

Message: CSHARP-1661: Refactoring.
Branch: v2.4.x
https://github.com/mongodb/mongo-csharp-driver/commit/2ea071e20ec976dd3bdfbc97977ac6fdcfc083cf

Comment by Githook User [ 21/Sep/16 ]

Author:

{u'username': u'craiggwilson', u'name': u'Craig Wilson', u'email': u'craiggwilson@gmail.com'}

Message: CSHARP-1661: support for $indexOfXXX.
Branch: v2.4.x
https://github.com/mongodb/mongo-csharp-driver/commit/da0f51acbb5fb4e99b2ae262de441b4a169b1daa

Comment by Githook User [ 16/Sep/16 ]

Author:

{u'username': u'craiggwilson', u'name': u'Craig Wilson', u'email': u'craiggwilson@gmail.com'}

Message: CSHARP-1661: added bytes and cp variants.
Branch: v2.4.x
https://github.com/mongodb/mongo-csharp-driver/commit/b8009a569c7d4cbbaa59771d45b5b6b097f86e68

Comment by Githook User [ 16/Sep/16 ]

Author:

{u'username': u'rstam', u'name': u'rstam', u'email': u'robert@robertstam.org'}

Message: CSHARP-1661: Refactoring.
Branch: v2.4.x
https://github.com/mongodb/mongo-csharp-driver/commit/5043e1bfb0cb62b238a8d4b2f52f24520ce50bc4

Comment by Githook User [ 16/Sep/16 ]

Author:

{u'username': u'craiggwilson', u'name': u'Craig Wilson', u'email': u'craiggwilson@gmail.com'}

Message: CSHARP-1661: support for $indexOfXXX.
Branch: v2.4.x
https://github.com/mongodb/mongo-csharp-driver/commit/9e12b4f2180b9729a33a4175aa0b3343abc19890

Comment by Githook User [ 09/Sep/16 ]

Author:

{u'username': u'craiggwilson', u'name': u'Craig Wilson', u'email': u'craiggwilson@gmail.com'}

Message: CSHARP-1661: added bytes and cp variants.
Branch: v2.4.x
https://github.com/mongodb/mongo-csharp-driver/commit/04614731897e289eb602862a9f809f46f4616292

Comment by Githook User [ 31/Aug/16 ]

Author:

{u'username': u'rstam', u'name': u'rstam', u'email': u'robert@robertstam.org'}

Message: CSHARP-1661: Refactoring.
Branch: v2.4.x
https://github.com/mongodb/mongo-csharp-driver/commit/2251a79c804ee2a3562e370b0e048f8b5f427b8c

Comment by Githook User [ 31/Aug/16 ]

Author:

{u'username': u'craiggwilson', u'name': u'Craig Wilson', u'email': u'craiggwilson@gmail.com'}

Message: CSHARP-1661: support for $indexOfXXX.
Branch: v2.4.x
https://github.com/mongodb/mongo-csharp-driver/commit/40b3316db0b483f69db3ef79a9d5da0e3ca52f2d

Comment by Githook User [ 29/Aug/16 ]

Author:

{u'username': u'rstam', u'name': u'rstam', u'email': u'robert@robertstam.org'}

Message: CSHARP-1661: Refactoring.
Branch: v2.4.x
https://github.com/mongodb/mongo-csharp-driver/commit/a6bbb4cd1e05ed384a751323680d38953d6c91aa

Comment by Githook User [ 29/Aug/16 ]

Author:

{u'username': u'craiggwilson', u'name': u'Craig Wilson', u'email': u'craiggwilson@gmail.com'}

Message: CSHARP-1661: support for $indexOfXXX.
Branch: v2.4.x
https://github.com/mongodb/mongo-csharp-driver/commit/3c7b4d7ae4031987ce169f62704772d1c0b74742

Comment by Githook User [ 29/Aug/16 ]

Author:

{u'username': u'rstam', u'name': u'rstam', u'email': u'robert@robertstam.org'}

Message: CSHARP-1661: Refactoring.
Branch: v2.4.x
https://github.com/mongodb/mongo-csharp-driver/commit/a7a166975384ad4a404fbcc8a8134a4cda3b2a07

Comment by Githook User [ 29/Aug/16 ]

Author:

{u'username': u'craiggwilson', u'name': u'Craig Wilson', u'email': u'craiggwilson@gmail.com'}

Message: CSHARP-1661: support for $indexOfXXX.
Branch: v2.4.x
https://github.com/mongodb/mongo-csharp-driver/commit/5497c55c8ba9b8c76c129ac6c96e47343a3ebf2c

Generated at Wed Feb 07 21:40:18 UTC 2024 using Jira 9.7.1#970001-sha1:2222b88b221c4928ef0de3161136cc90c8356a66.