Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Kotlin class support for findOneAndUpdate in CoroutineCollection #396

Merged
merged 1 commit into from
Apr 3, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ import org.bson.Document
import org.bson.types.ObjectId
import org.junit.Test
import org.litote.kmongo.MongoOperator
import org.litote.kmongo.eq
import org.litote.kmongo.json
import org.litote.kmongo.model.ExposableFriend
import org.litote.kmongo.model.Friend
Expand Down Expand Up @@ -57,6 +58,28 @@ class ReactiveStreamsFindOneAndModifyTest : KMongoReactiveStreamsCoroutineBaseTe
}
}

@Test
fun `can find and update with kotlin class`() = runBlocking {
col.insertOne(Friend("John", "22 Wall Street Avenue"))
col.findOneAndUpdate(Friend::name eq "John", Friend("John", "A better place"))
?: throw AssertionError("Value cannot null!")
val savedFriend = col.findOne("{name:'John'}") ?: throw AssertionError("Value cannot null!")
assertEquals("John", savedFriend.name)
assertEquals("A better place", savedFriend.address)
}

@Test
fun `can find and update with kotlin class in ClientSession`() = runBlocking {
mongoClient.startSession().use {
col.insertOne(it, Friend("John", "22 Wall Street Avenue"))
col.findOneAndUpdate(it, Friend::name eq "John", Friend("John", "A better place"))
?: throw AssertionError("Value cannot null!")
val savedFriend = col.findOne(it, "{name:'John'}") ?: throw AssertionError("Value cannot null!")
assertEquals("John", savedFriend.name)
assertEquals("A better place", savedFriend.address)
}
}

@Test
fun canFindAndUpdateWithNullValue() = runBlocking {
col.insertOne(Friend("John", "22 Wall Street Avenue"))
Expand Down Expand Up @@ -145,4 +168,4 @@ class ReactiveStreamsFindOneAndModifyTest : KMongoReactiveStreamsCoroutineBaseTe
assertEquals(1, countBobs)
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -764,6 +764,42 @@ class CoroutineCollection<T : Any>(val collection: MongoCollection<T>) {
options: FindOneAndUpdateOptions = FindOneAndUpdateOptions()
): T? = collection.findOneAndUpdate(clientSession, filter, update, options).awaitFirstOrNull()

/**
* Atomically find a document and update it.
*
* @param filter a document describing the query filter, which may not be null.
* @param update a document describing the update, which may not be null. The update to apply must include only update operators.
* @param options the options to apply to the operation
* @return the document that was updated. Depending on the value of the `returnOriginal`
* property, this will either be the document as it was before the update or as it is after the update. If no documents matched the
* query filter, then null will be returned
*/
suspend fun findOneAndUpdate(
filter: Bson,
update: T,
options: FindOneAndUpdateOptions = FindOneAndUpdateOptions()
): T? = collection.findOneAndUpdate(filter, toBsonModifier(update), options).awaitFirstOrNull()

/**
* Atomically find a document and update it.
*
* @param clientSession the client session with which to associate this operation
* @param filter a document describing the query filter, which may not be null.
* @param update a document describing the update, which may not be null. The update to apply must include only update operators.
* @param options the options to apply to the operation
* @return a publisher with a single element the document that was updated. Depending on the value of the `returnOriginal`
* property, this will either be the document as it was before the update or as it is after the update. If no documents matched the
* query filter, then null will be returned
* @mongodb.server.release 3.6
* @since 1.7
*/
suspend fun findOneAndUpdate(
clientSession: ClientSession,
filter: Bson,
update: T,
options: FindOneAndUpdateOptions = FindOneAndUpdateOptions()
): T? = collection.findOneAndUpdate(clientSession, filter, toBsonModifier(update), options).awaitFirstOrNull()

/**
* Drops this collection from the Database.
*
Expand Down