Skip to content

Commit

Permalink
Adding ability to use setTo in updateOneById
Browse files Browse the repository at this point in the history
  • Loading branch information
PeredurOmega authored and zigzago committed Mar 27, 2022
1 parent d0133a9 commit 17026c2
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 0 deletions.
10 changes: 10 additions & 0 deletions kmongo-core-tests/src/main/kotlin/org/litote/kmongo/UpdateTest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,16 @@ class UpdateTest : AllCategoriesKMongoBaseTest<Friend>() {
assertEquals(friend._id, r._id)
}

@Test
fun canUpdateWithSetToByObjectId() {
val friend = Friend("Paul")
col.insertOne(friend)
col.updateOneById(friend._id!!, Friend::name setTo "John")
val r = col.findOne("{name:'John'}")
assertEquals("John", r!!.name)
assertEquals(friend._id, r._id)
}

@Test
fun canUpsert() {
col.updateOne("{}", "{$set:{name:'John'}}", UpdateOptions().upsert(true))
Expand Down
19 changes: 19 additions & 0 deletions kmongo-core/src/main/kotlin/org/litote/kmongo/MongoCollections.kt
Original file line number Diff line number Diff line change
Expand Up @@ -527,6 +527,25 @@ fun <T> MongoCollection<T>.updateOneById(
): UpdateResult =
updateOne(KMongoUtil.idFilterQuery(id), KMongoUtil.toBsonModifier(update, updateOnlyNotNullProperties), options)

/**
* Update a single document in the collection according to the specified arguments.
*
* @param id the object id
* @param updates the setTo describing the updates
* @param options the options to apply to the update operation
*
* @return the result of the update one operation
*
* @throws com.mongodb.MongoWriteException if the write failed due some other failure specific to the update command
* @throws com.mongodb.MongoWriteConcernException if the write failed due being unable to fulfil the write concern
* @throws com.mongodb.MongoException if the write failed due some other failure
*/
fun <T : Any> MongoCollection<T>.updateOneById(
id: Any,
vararg updates: SetTo<*>,
options: UpdateOptions = UpdateOptions()
): UpdateResult = updateOne(KMongoUtil.idFilterQuery(id), set(*updates), options)

/**
* Update all documents in the collection according to the specified arguments.
*
Expand Down

0 comments on commit 17026c2

Please sign in to comment.