private static int doUpdate()

in mongodb/src/main/java/org/hibernate/ogm/datastore/mongodb/MongoDBDialect.java [1374:1414]


	private static int doUpdate(final MongoDBQueryDescriptor queryDesc, final MongoCollection<Document> collection, MongoDBQueryDescriptor.Operation operation) {
		Document query = queryDesc.getCriteria();
		Document update = queryDesc.getUpdateOrInsertOne();
		Document options = queryDesc.getOptions();
		Boolean upsert = FALSE;
		Boolean multi = FALSE;
		WriteConcern wc = null;
		if ( options != null ) {
			upsert = (Boolean) options.get( "upsert" );
			upsert = ( upsert != null ) ? upsert : FALSE;
			multi = (Boolean) options.get( "multi" );
			multi = ( multi != null ) ? multi : FALSE;
			Document o = (Document) options.get( "writeConcern" );
			wc = getWriteConcern( o );
		}
		UpdateOptions updateOptions = new UpdateOptions().upsert( upsert );
		UpdateResult result = null;
		switch ( operation ) {
			case UPDATEONE:
				result = collection.withWriteConcern( ( wc != null ? wc : collection.getWriteConcern() ) ).updateOne( query, update, updateOptions );
				break;
			case UPDATEMANY:
				result = collection.withWriteConcern( ( wc != null ? wc : collection.getWriteConcern() ) ).updateMany( query, update, updateOptions );
				break;
			case UPDATE:
				if ( multi ) {
					result = collection.withWriteConcern( ( wc != null ? wc : collection.getWriteConcern() ) ).updateMany( query, update, updateOptions );
				}
				else {
					result = collection.withWriteConcern( ( wc != null ? wc : collection.getWriteConcern() ) ).updateOne( query, update, updateOptions );
				}
		}

		if ( result.wasAcknowledged() ) {
			// IMPROVE How could we return result.getUpsertedId() if it was an upsert, or isUpdateOfExisting()?
			// I see only a possibility by using javax.persistence.StoredProcedureQuery in the application
			// and then using getOutputParameterValue(String) to get additional result values.
			return (int) result.getModifiedCount();
		}
		return -1; // Not sure if we should throw an exception instead?
	}