title: Mongodb Java - Update document replacing a string in field
body: <p>I am looking for a way to update a document replacing a substring of a field with another value.<br>
  For example, in my database i have the following field:<br></p>
  <pre><code>{
      &quot;_id&quot;: 1,
      &quot;value&quot;: &quot;Old text in the document&quot;
  }
  </code></pre>
  <p>To update the field i would simply do somehting like this</p>
  <pre><code>        MongoClient client = ...init client mongodb
          try (ClientSession session = MongoSession.createSession(client)) {
              session.startTransaction(TransactionOptions.builder().writeConcern(WriteConcern.MAJORITY).build());
              MongoDatabase database = client.getDatabase(&quot;test_db&quot;);
  
              MongoCollection&lt;FileEntity&gt; filesDB = database.getCollection(&quot;my_collection&quot;, MyClass.class);
              Bson query = Filters.and(Filters.eq(&quot;_id&quot;, 1));
              Bson updates = Updates.set(&quot;value&quot;, &quot;New text in the document&quot;);
  
              filesDB.updateOne(session, query, updates);
              session.commitTransaction();
          } catch (Exception e) {
              logger.error(&quot;Error: &quot;, e);
          } finally {
              logger.debug(&quot;Success&quot;);
          }
  </code></pre>
  <p>And this would result in:</p>
  <pre><code>{
      &quot;_id&quot;: 1,
      &quot;value&quot;: &quot;New text in the document&quot;
  }
  </code></pre>
  <p>But this would require that i know the current value of the field, but in my case i don't.<br>
  So i am trying to find a way to say something like</p>
  <pre><code>Bson updates = Updates.set(&quot;value&quot;, &quot;$value.replace(\&quot;Old\&quot;, \&quot;New\&quot;)&quot;);
  </code></pre>
  <p>Is this possible?<br>
  The java driver of mongodb i have is version 3.12.10 and my mongodb server version is v4.2.12.<br>
  Thanks!</p>