I want to remove a string from array of strings of a mongodb model, if the string contains substring value
The model is like this
{
"username": "my_user",
"str_array": [
"remove_substring_found/rest_of_string",
"this_string_wont_be_removed"
]
}
Here is what I tried so far, but it is not working
string compare_str = "remove_substring_found";
var filter = Builders<MyModel>.Filter.Eq(s => s.username, "my_user");
var regex = new BsonRegularExpression(string.Format(".*{0}.*", compare_str ), "i");
var filter_update = Builders<string>.Filter.Regex(x => x, regex);
var update = Builders<MyModel>.Update.PullFilter(x => x.str_array, filter_update);
var result = await MyCollection.UpdateOneAsync(filter, update);