Skip to content
Snippets Groups Projects
Commit 8f2629ff authored by Felipe D'Abrantes's avatar Felipe D'Abrantes
Browse files

Add mongo function to update a document

parent a327bedd
No related branches found
No related tags found
1 merge request!14Add endpoints to manage Dailies
...@@ -5,6 +5,7 @@ import utils.ConfigHelper ...@@ -5,6 +5,7 @@ import utils.ConfigHelper
import org.mongodb.scala.{MongoClient, MongoDatabase, MongoCollection, Document, FindObservable, Observer, Observable} import org.mongodb.scala.{MongoClient, MongoDatabase, MongoCollection, Document, FindObservable, Observer, Observable}
import org.mongodb.scala.model.{Filters, Projections, Sorts} import org.mongodb.scala.model.{Filters, Projections, Sorts}
import org.bson.conversions.Bson import org.bson.conversions.Bson
import org.bson.types.ObjectId
import scala.concurrent.ExecutionContext.Implicits.global import scala.concurrent.ExecutionContext.Implicits.global
import scala.concurrent.Future import scala.concurrent.Future
...@@ -93,4 +94,23 @@ class MongoDBClient { ...@@ -93,4 +94,23 @@ class MongoDBClient {
} }
}) })
} }
/**
* Updates a document in a MongoDB collection.
*
* @param collection The MongoCollection instance the document is in.
* @param documentId The ID of the document to update.
* @param updates A sequence of Bson documents defining the updates.
* @throws RuntimeException if the update was not acknowledged by the database.
*/
def updateOne(collection: MongoCollection[Document], documentId: ObjectId, updates: Seq[Bson]): Future[Unit] = {
val filter = Filters.equal("_id", documentId)
val futureResult = collection.updateOne(filter, updates).toFuture()
futureResult.map(result => {
if (!result.wasAcknowledged()) {
throw new RuntimeException("Update was not acknowledged")
}
})
}
} }
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment