Skip to content
GitLab
Explore
Sign in
Register
Primary navigation
Search or go to…
Project
D
Daily Thought App
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Iterations
Wiki
Requirements
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Locked files
Build
Pipelines
Jobs
Pipeline schedules
Test cases
Artifacts
Deploy
Package registry
Model registry
Operate
Environments
Terraform modules
Monitor
Incidents
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Code review analytics
Issue analytics
Insights
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Terms and privacy
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
COM3014-Coursework
Daily Thought App
Commits
8f2629ff
Commit
8f2629ff
authored
2 years ago
by
Felipe D'Abrantes
Browse files
Options
Downloads
Patches
Plain Diff
Add mongo function to update a document
parent
a327bedd
No related branches found
No related tags found
1 merge request
!14
Add endpoints to manage Dailies
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
backend-services/feed-service/app/models/MongoDBClient.scala
+20
-0
20 additions, 0 deletions
backend-services/feed-service/app/models/MongoDBClient.scala
with
20 additions
and
0 deletions
backend-services/feed-service/app/models/MongoDBClient.scala
+
20
−
0
View file @
8f2629ff
...
...
@@ -5,6 +5,7 @@ import utils.ConfigHelper
import
org.mongodb.scala.
{
MongoClient
,
MongoDatabase
,
MongoCollection
,
Document
,
FindObservable
,
Observer
,
Observable
}
import
org.mongodb.scala.model.
{
Filters
,
Projections
,
Sorts
}
import
org.bson.conversions.Bson
import
org.bson.types.ObjectId
import
scala.concurrent.ExecutionContext.Implicits.global
import
scala.concurrent.Future
...
...
@@ -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"
)
}
})
}
}
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment