vertx/src/main/java/eu/internetofus/common/vertx/Repository.java
/**
   * Add or update one document.
   *
   * @param collectionName of the collections that contains the model to update.
   * @param query          to to match the document to update.
   * @param updateModel    the new values of the model.
   * @param upsert         is {@code true} if has to add if not defined.
   *
   * @return the future result of the update action with a {@code null} value if
   *         the model is updated or with the identifier of the added model.
   */
Add or update one document.
@param collectionName of the collections that contains the model to update.

@return the future result of the update action with a  null value if
the model is updated or with the identifier of the added model.
Add or update one document . @param collectionName of the collections that contains the model to update . @return the future result of the update action with a null value if the model is updated or with the identifier of the added model .
protected Future<String> upsertOneDocument(@NotNull final String collectionName, @NotNull final JsonObject query,
      final JsonObject updateModel, final boolean upsert) {
    if (updateModel == null) {
      return Future.failedFuture("Not found document to update");
    } else {
      updateModel.remove("_creationTs");
      final var setFields = new JsonObject().put(SCHEMA_VERSION, this.schemaVersion);
      final var updateQuery = new JsonObject();
      updateQuery.put("$set", setFields);
      final var unsetFields = new JsonObject();
      for (final String fieldName : updateModel.fieldNames()) {
        final var fieldValue = updateModel.getValue(fieldName);
        if (fieldValue != null) {
          setFields.put(fieldName, fieldValue);
        } else {
          unsetFields.put(fieldName, "");
        }
      }
      if (!unsetFields.isEmpty()) {
        updateQuery.put("$unset", unsetFields);
      }
      final var options = new UpdateOptions().setMulti(false).setUpsert(upsert);
      return this.pool.updateCollectionWithOptions(collectionName, query, updateQuery, options).compose(result -> {
        if (result.getDocModified() != 1) {
          if (upsert) {
            String _id = null;
            final var id = result.getDocUpsertedId();
            if (id != null) {
              _id = id.getString("_id");
            }
            if (_id == null && result.getDocMatched() != 1) {
              return Future.failedFuture("Not added document");
            } else {
              return Future.succeededFuture(_id);
            }
          } else {
            return Future.failedFuture("Not found document to update");
          }
        } else {
          return Future.succeededFuture();
        }
      });
    }
  }
protected Future < String > upsertOneDocument ( @ NotNull final String collectionName , @ NotNull final JsonObject query , final JsonObject updateModel , final boolean upsert ) { if ( updateModel == null ) { return Future . failedFuture ( "Not found document to update" ) ; } else { updateModel . remove ( "_creationTs" ) ; final var setFields = new JsonObject ( ) . put ( SCHEMA_VERSION , this . schemaVersion ) ; final var updateQuery = new JsonObject ( ) ; updateQuery . put ( "$set" , setFields ) ; final var unsetFields = new JsonObject ( ) ; for ( final String fieldName : updateModel . fieldNames ( ) ) { final var fieldValue = updateModel . getValue ( fieldName ) ; if ( fieldValue != null ) { setFields . put ( fieldName , fieldValue ) ; } else { unsetFields . put ( fieldName , "" ) ; } } if ( ! unsetFields . isEmpty ( ) ) { updateQuery . put ( "$unset" , unsetFields ) ; } final var options = new UpdateOptions ( ) . setMulti ( false ) . setUpsert ( upsert ) ; return this . pool . updateCollectionWithOptions ( collectionName , query , updateQuery , options ) . compose ( result -> { if ( result . getDocModified ( ) != 1 ) { if ( upsert ) { String _id = null ; final var id = result . getDocUpsertedId ( ) ; if ( id != null ) { _id = id . getString ( "_id" ) ; } if ( _id == null && result . getDocMatched ( ) != 1 ) { return Future . failedFuture ( "Not added document" ) ; } else { return Future . succeededFuture ( _id ) ; } } else { return Future . failedFuture ( "Not found document to update" ) ; } } else { return Future . succeededFuture ( ) ; } } ) ; } }
Add or update one document.
Add or update one document .
// NO modify the _creationTs. // Create the query to update the model.