UpsertQuery¶
interface UpsertQuery<T> : UpdateQuery<T>
DSL for MongoDB operators that are used to update existing values, creating new documents if none exist (does not include aggregation operators).
This interface is a variant of UpdateQuery used in upsert operations. See UpdateQuery for more information.
If you can't find the operator you're searching for, visit the tracking issue.
Constructors¶
UpsertQuery¶
@LowLevelApi
fun <T> UpsertQuery(context: BsonContext): UpsertQuery<T>
Creates an empty UpsertQuery.
Properties¶
all¶
The all positional operator: selects all elements of an array.
This operator is used to declare an update that applies to all items of an array.
Example¶
class User(
val name: String,
val pets: List<Pet>,
)
class Pet(
val name: String,
val age: Int,
)
users.updateMany {
User::pets.all / Pet::age inc 1
}
External resources¶
open val <E> KProperty1<T, Collection<E>>.all: Field<T, E>
The all positional operator: selects all elements of an array.
This operator is used to declare an update that applies to all items of an array.
Example¶
class User(
val name: String,
val pets: List<Pet>,
)
class Pet(
val name: String,
val age: Int,
)
users.updateMany {
User::pets.all / Pet::age inc 1
}
External resources¶
context¶
@LowLevelApi
abstract val context: BsonContext
The context used to generate this expression.
field¶
Converts a Kotlin property into a Field.
selected¶
The positional operator: update an array item selected in the filter.
When we use any or anyValue in a filter to select an item, we can use this operator to update whichever item was selected.
Do not use this operator in an upsert.
Example¶
class User(
val name: String,
val pets: List<Pet>,
)
class Pet(
val name: String,
val age: Int,
)
users.updateMany(
filter = {
User::pets.any / Pet::name eq "Bobby"
},
update = {
User::pets.selected / Pet::age inc 1
}
)
This example finds all users who have a pet named "Bobby", and increases its age by 1. Note that if the users have other pets, they are not impacted.
External resources¶
open val <E> KProperty1<T, Collection<E>>.selected: Field<T, E>
The positional operator: update an array item selected in the filter.
When we use any or anyValue in a filter to select an item, we can use this operator to update whichever item was selected.
Do not use this operator in an upsert.
Example¶
class User(
val name: String,
val pets: List<Pet>,
)
class Pet(
val name: String,
val age: Int,
)
users.updateMany(
filter = {
User::pets.any / Pet::name eq "Bobby"
},
update = {
User::pets.selected / Pet::age inc 1
}
)
This example finds all users who have a pet named "Bobby", and increases its age by 1. Note that if the users have other pets, they are not impacted.
External resources¶
Functions¶
accept¶
Adds a new node as a child of this one.
acceptAll¶
Adds any number of nodes into this one.
addToSet¶
Adds value at the end of the array, unless it is already present, in which case it does nothing.
MongoDB detects equality if the two documents are exactly identical. All the fields must be the same in the same order.
Example¶
class User(
val name: String,
val age: Int,
val tokens: List<String>,
)
collection.updateOne(
filter = {
User::name eq "Bob"
},
update = {
User::tokens addToSet "123456789"
}
)
This will add "123456789" to the user's tokens only if it isn't already present.
External resources¶
open infix fun <V> KProperty1<T, Collection<V>>.addToSet(value: V)
Adds value at the end of the array, unless it is already present, in which case it does nothing.
MongoDB detects equality if the two documents are exactly identical. All the fields must be the same in the same order.
Example¶
class User(
val name: String,
val age: Int,
val tokens: List<String>,
)
collection.updateOne(
filter = {
User::name eq "Bob"
},
update = {
User::tokens addToSet "123456789"
}
)
This will add "123456789" to the user's tokens only if it isn't already present.
External resources¶
Adds multiple values at the end of the array, unless they are already present.
Each value in values is treated independently. It if it is already present in the array, nothing happens. If it is absent from the array, it is added at the end.
MongoDB detects equality if the two documents are exactly identical. All the fields must be the same in the same order.
This is a convenience function for calling addToSet multiple times.
Example¶
class User(
val name: String,
val age: Int,
val tokens: List<String>,
)
collection.updateOne(
filter = {
User::name eq "Bob"
},
update = {
User::tokens addToSet listOf("123456789", "789456123")
}
)
This will add "123456789" and "789465123" to the user's tokens only if they aren't already present. If only one of them is present, the other is added.
External resources¶
open infix fun <V> KProperty1<T, Collection<V>>.addToSet(values: Iterable<V>)
Adds multiple values at the end of the array, unless they are already present.
Each value in values is treated independently. It if it is already present in the array, nothing happens. If it is absent from the array, it is added at the end.
MongoDB detects equality if the two documents are exactly identical. All the fields must be the same in the same order.
This is a convenience function for calling addToSet multiple times.
Example¶
class User(
val name: String,
val age: Int,
val tokens: List<String>,
)
collection.updateOne(
filter = {
User::name eq "Bob"
},
update = {
User::tokens addToSet listOf("123456789", "789456123")
}
)
This will add "123456789" and "789465123" to the user's tokens only if they aren't already present. If only one of them is present, the other is added.
External resources¶
div¶
Refers to child as a nested field of the current field.
Refers to child as a nested field of the current field.
Refers to child as a nested field of the current field.
freeze¶
@LowLevelApi
abstract override fun freeze()
Makes this expression immutable.
get¶
Refers to a specific item in an array, by its index.
Refers to a specific item in a map, by its name.
Refers to a specific item in an array, by its index.
Refers to a specific item in a map, by its name.
inc¶
Increments a field by the specified amount.
amount may be negative, in which case the field is decremented.
If the field doesn't exist (either the document doesn't have it, or the operation is an upsert and a new document is created), the field is created with an initial value of amount.
Use of this operator with a field with a null value will generate an error.
Example¶
class User(
val name: String,
val age: Int,
)
// It's the new year!
collection.updateMany {
User::age inc 1
}
External resources¶
Increments a field by the specified amount.
amount may be negative, in which case the field is decremented.
If the field doesn't exist (either the document doesn't have it, or the operation is an upsert and a new document is created), the field is created with an initial value of amount.
Use of this operator with a field with a null value will generate an error.
Example¶
class User(
val name: String,
val age: Int,
)
// It's the new year!
collection.updateMany {
User::age inc 1
}
External resources¶
max¶
Updates the value of a field to the specified value only if the specified value is greater than the current value of the field.
If the field doesn't exist, the field is set to the specified value.
Example¶
External resources¶
open infix fun <V : Comparable<V>> KProperty1<T, V?>.max(value: V)
Updates the value of a field to the specified value only if the specified value is greater than the current value of the field.
If the field doesn't exist, the field is set to the specified value.
Example¶
External resources¶
min¶
Updates the value of a field to the specified value only if the specified value is less than the current value of the field.
If the field doesn't exist, the field is set to the specified value.
Example¶
External resources¶
open infix fun <V : Comparable<V>> KProperty1<T, V?>.min(value: V)
Updates the value of a field to the specified value only if the specified value is less than the current value of the field.
If the field doesn't exist, the field is set to the specified value.
Example¶
External resources¶
mul¶
Multiplies a field by the specified amount.
If the field doesn't exist (either the document doesn't have it, or the operation is an upsert and a new document is created), the field is created with an initial value of 0.
Use of this operator with a field with a null value will generate an error.
Example¶
External resources¶
Multiplies a field by the specified amount.
If the field doesn't exist (either the document doesn't have it, or the operation is an upsert and a new document is created), the field is created with an initial value of 0.
Use of this operator with a field with a null value will generate an error.
Example¶
External resources¶
plusAssign¶
Increments a field by the specified amount.
amount may be negative, in which case the field is decremented.
If the field doesn't exist (either the document doesn't have it, or the operation is an upsert and a new document is created), the field is created with an initial value of amount.
Use of this operator with a field with a null value will generate an error.
Example¶
class User(
val name: String,
val age: Int,
)
// It's the new year!
collection.updateMany {
User::age += 1
}
External resources¶
open operator fun <V : Number> KProperty1<T, V>.plusAssign(amount: V)
Increments a field by the specified amount.
amount may be negative, in which case the field is decremented.
If the field doesn't exist (either the document doesn't have it, or the operation is an upsert and a new document is created), the field is created with an initial value of amount.
Use of this operator with a field with a null value will generate an error.
Example¶
class User(
val name: String,
val age: Int,
)
// It's the new year!
collection.updateMany {
User::age += 1
}
External resources¶
renameTo¶
Renames a field.
Example¶
class User(
val name: String,
val age: Int,
val ageOld: Int,
)
collection.updateMany {
User::ageOld renameTo User::age
}
External resources¶
Renames a field.
Example¶
class User(
val name: String,
val age: Int,
val ageOld: Int,
)
collection.updateMany {
User::ageOld renameTo User::age
}
External resources¶
Renames a field.
Example¶
class User(
val name: String,
val age: Int,
val ageOld: Int,
)
collection.updateMany {
User::ageOld renameTo User::age
}
External resources¶
open infix fun <V> KProperty1<T, V>.renameTo(newName: KProperty1<T, V>)
Renames a field.
Example¶
class User(
val name: String,
val age: Int,
val ageOld: Int,
)
collection.updateMany {
User::ageOld renameTo User::age
}
External resources¶
set¶
Replaces the value of a field with the specified value.
Example¶
class User(
val name: String?,
val age: Int,
)
collection.filter {
User::name eq "foo"
}.updateMany {
User::age set 18
}
External resources¶
See also
UpsertQuery.setOnInsert: Only set if a new document is created.
Replaces the value of a field with the specified value.
Example¶
class User(
val name: String?,
val age: Int,
)
collection.filter {
User::name eq "foo"
}.updateMany {
User::age set 18
}
External resources¶
See also
UpsertQuery.setOnInsert: Only set if a new document is created.
setIf¶
Replaces the value of a field with the specified value, if condition is true.
If condition is false, this operator does nothing.
Example¶
class User(
val name: String?,
val age: Int,
)
collection.filter {
User::name eq "foo"
}.updateMany {
User::age.setIf(someComplexOperation, 18)
}
External resources¶
See also
UpsertQuery.setOnInsert: Only set if a new document is created.
Replaces the value of a field with the specified value, if condition is true.
If condition is false, this operator does nothing.
Example¶
class User(
val name: String?,
val age: Int,
)
collection.filter {
User::name eq "foo"
}.updateMany {
User::age.setIf(someComplexOperation, 18)
}
External resources¶
See also
UpsertQuery.setOnInsert: Only set if a new document is created.
setOnInsert¶
If an upsert operation results in an insert of a document, then this operator assigns the specified value to the field. If the update operation does not result in an insert, this operator does nothing.
Example¶
class User(
val name: String?,
val age: Int,
)
collection.filter {
User::name eq "foo"
}.upsertOne {
User::age setOnInsert 18
}
External resources¶
See also
UpsertQuery.set: Always set the value.
open infix fun <V> KProperty1<T, V>.setOnInsert(value: V)
If an upsert operation results in an insert of a document, then this operator assigns the specified value to the field. If the update operation does not result in an insert, this operator does nothing.
Example¶
class User(
val name: String?,
val age: Int,
)
collection.filter {
User::name eq "foo"
}.upsertOne {
User::age setOnInsert 18
}
External resources¶
See also
UpsertQuery.set: Always set the value.
setToCurrentDate¶
abstract fun Field<T, Instant?>.setToCurrentDate()
Sets this field to the current date.
If the field does not exist, this operator adds the field to the document.
Example¶
class User(
val name: String?,
val age: Int,
val modificationDate: Instant,
)
collection.filter {
User::name eq "Bob"
}.updateMany {
User::age set 18
User::modificationDate.setToCurrentDate()
}
External resources¶
open fun KProperty1<T, Instant?>.setToCurrentDate()
Sets this field to the current date.
If the field does not exist, this operator adds the field to the document.
Example¶
class User(
val name: String?,
val age: Int,
val modificationDate: Instant,
)
collection.filter {
User::name eq "Bob"
}.updateMany {
User::age set 18
User::modificationDate.setToCurrentDate()
}
External resources¶
@JvmName
(name = "setToCurrentTimestamp")abstract fun Field<T, Timestamp?>.setToCurrentDate()
Sets this field to the current timestamp.
If the field does not exist, this operator adds the field to the document.
The time Timestamp is internal. Instant should be preferred. For more information, see Timestamp.
Example¶
class User(
val name: String?,
val age: Int,
val modificationDate: Timestamp,
)
collection.filter {
User::name eq "Bob"
}.updateMany {
User::age set 18
User::modificationDate.setToCurrentDate()
}
External resources¶
@JvmName
(name = "setToCurrentTimestamp")open fun KProperty1<T, Timestamp?>.setToCurrentDate()
Sets this field to the current timestamp.
If the field does not exist, this operator adds the field to the document.
The time Timestamp is internal. Instant should be preferred. For more information, see Timestamp.
Example¶
class User(
val name: String?,
val age: Int,
val modificationDate: Timestamp,
)
collection.filter {
User::name eq "Bob"
}.updateMany {
User::age set 18
User::modificationDate.setToCurrentDate()
}
External resources¶
setUnless¶
Replaces the value of a field with the specified value, if condition is false.
If condition is true, this operator does nothing.
Example¶
class User(
val name: String?,
val age: Int,
)
collection.filter {
User::name eq "foo"
}.updateMany {
User::age.setUnless(someComplexOperation, 18)
}
External resources¶
See also
UpsertQuery.setOnInsert: Only set if a new document is created.
Replaces the value of a field with the specified value, if condition is false.
If condition is true, this operator does nothing.
Example¶
class User(
val name: String?,
val age: Int,
)
collection.filter {
User::name eq "foo"
}.updateMany {
User::age.setUnless(someComplexOperation, 18)
}
External resources¶
See also
UpsertQuery.setOnInsert: Only set if a new document is created.
simplify¶
Returns a simplified (but equivalent) expression to the current expression.
toBson¶
Writes the result of simplifying to a new BSON document.
toString¶
JSON representation of this expression.
unsafe¶
Refers to a field child of the current field, with no compile-time safety.
open infix fun <Root, Child> KProperty1<Root, *>.unsafe(child: KProperty1<*, Child>): Field<Root, Child>
Refers to a field child of the current field, without checking that it is a field available on the current object.
Refers to a field child of the current field, without checking that it is a field available on the current object.
Refers to a field child of the current field, without checking that it is a field available on the current object.
Refers to a field child of the current field, without checking that it is a field available on the current object.
Refers to a field child of the current field, with no compile-time safety.
unset¶
Deletes a field.
Example¶
class User(
val name: String,
val age: Int,
val alive: Boolean,
)
collection.filter {
User::name eq "Luke Skywalker"
}.updateOne {
User::age.unset()
User::alive set false
}
External resources¶
open fun <V> KProperty1<T, V>.unset()
Deletes a field.
Example¶
class User(
val name: String,
val age: Int,
val alive: Boolean,
)
collection.filter {
User::name eq "Luke Skywalker"
}.updateOne {
User::age.unset()
User::alive set false
}
External resources¶
writeTo¶
@LowLevelApi
abstract override fun writeTo(writer: BsonFieldWriter)
Writes the result of simplifying this expression into writer.