Find character position in string

I need help in mongodb to update record as explained below.

original text in “name” field of a document is “This is sample text (with some text into bracket)” and
I need to update it to “This is sample text”.
I want to remove everything inside bracket including bracket.

I am using following query in mongodb studio 3T intellisense

db.tracks.update(
   {"_id" : ObjectId("5d91dabf0413c90008b39c3e")},
   {$set : { "name" : "This is sample text" }},
   {multi : true}
)

I want to use substr to get text upto opening bracket position, but question is - how to find variable position of opening bracket “(” for substr function and how to use substr.

Regards,

Hi @Rushi_Pandya ,

Welcome to MongoDB Community.

I think you can use $indexOfBytes aggregation operation with a 4.2 feature called pipeline updates:

db.tracks.update( {"_id" : ObjectId("5d91dabf0413c90008b39c3e")},
  [{$set : {name : 
  { $substr: ["$text",0,{ $indexOfBytes : ["$text", "("] }]}
  }}],{multi : true});

If your version is prior to 4.2 you will need to query with aggregate and update in a second statement.

Best regards,
Pavel