This repository has been archived by the owner on Oct 2, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
docs(website): Migrate to updateMany, updateOne, upsertOne
- Loading branch information
Showing
1 changed file
with
91 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,91 @@ | ||
# Update data | ||
|
||
Much like [find variants](search.md), update operations use a DSL instead of passing raw BSON values. | ||
|
||
## Updating multiple documents | ||
|
||
Like with KMongo, the function to update multiple documents is `updateMany`. | ||
|
||
```kotlin title="With KMongo" | ||
collection.updateMany( | ||
filter = and( | ||
User::name.exists(), | ||
User::age gt 18 | ||
), | ||
set( | ||
User::isLegal setTo true | ||
) | ||
) | ||
``` | ||
|
||
```kotlin title="With KtMongo" | ||
collection.updateMany( | ||
filter = { | ||
User::name.exists() | ||
User::age gt 18 | ||
}, | ||
update = { | ||
User::isLegal set true | ||
} | ||
) | ||
``` | ||
|
||
To learn more about filtering, visit [the find documentation](search.md). | ||
|
||
Unlike in KMongo, there is no need to combine multiple operators yourself, the library will do it for you. The order of operators is not relevant. | ||
|
||
```kotlin title="With KMongo" | ||
collection.updateMany( | ||
filter = …, | ||
set( | ||
User::name setTo "foo", | ||
User::isLegal setTo true, | ||
), | ||
inc( | ||
User::age setTo 1 | ||
) | ||
) | ||
``` | ||
|
||
```kotlin title="With KtMongo" | ||
collection.updateMany( | ||
filter = { … }, | ||
update = { | ||
User::name set "foo" | ||
User::isLegal set true | ||
User::age inc 1 | ||
} | ||
) | ||
``` | ||
|
||
## Updating a single document | ||
|
||
Like in KMongo, KtMongo provides a dedicated function to edit a single element, `updateOne`. | ||
|
||
## Inserting a document if it doesn't exist | ||
|
||
KtMongo provides an overload to perform upserts: | ||
|
||
```kotlin title="With KMongo" | ||
collection.updateOne( | ||
filter = …, | ||
setOnInsert( | ||
User::creationDate setTo Instant.now(), | ||
), | ||
UpdateOptions().upsert(true) | ||
) | ||
``` | ||
|
||
```kotlin title="With KtMongo" | ||
collection.upsertOne( | ||
filter = { … }, | ||
update = { | ||
User::creationDate setOnInsert Instant.now() | ||
} | ||
) | ||
``` | ||
|
||
!!! tip | ||
It is also possible to perform an upsert by setting `UpdateOptions().upsert(true)`, as well as setting any other update options, with `updateOne` and `updateMany`, just like in KMongo. `upsertOne` is just syntactic sugar, since we find that we use it very often. | ||
|
||
Note that options are often the first parameter instead of the last (because the last is the lambda block). Other than that, they behave identically. |