Update API
Math operators, field operators and array operators.
Although it is possible in MongoDB to use the direct field updates (no operator, {age:5}),this is not supported in TyDB to enforce a more strict type declaration.
Field operators
$set
$setApplies to: any field type.
Syntax:
{
$set: {
<fieldName1>: <value1>,
<fieldName2>: <value2>,
...etc
}
}Explanation: Sets the value of a field in a document.
/**
* Update a document where age is 1
* to age 5 and name: "alex"
*/
db.update({
filter: {age: 1},
update: {
$set: {
age: 5,
name: "alex"
}
}
});$unset
$unsetApplies to: any field type.
Syntax:
{
$unset: {
<fieldName1>: "",
<fieldName2>: "",
...etc
}
}Explanation: Sets the specified field to undefined.
/**
* Sets the "lastLogin" field to undefiend
* for documents where age is 1
*/
db.update({
filter: {age: 1},
update: {
$unset: {
lastLogin: ""
}
}
});$setOnInsert
$setOnInsertApplies to: any field type.
Syntax:
Explanation: If an upsert operation has resulted in an insert of a document, then $setOnInsert assigns the specified values to the fields in the document. If the update operation does not result in an insert, $setOnInsert does nothing. $setOnInsert can only take a full model, so you must use Model.new({ ... props }) with it.
$rename
$renameApplies to: any field type.
Syntax:
Explanation: renames the specified field to a new field name.
$currentDate
$currentDateApplies to: number or Date.
Syntax:
Explanation: Sets the value of a field in a document to the current timestamp or Date object.
Mathematical operators
$inc
$incApplies to: number.
Syntax:
Explanation: Increments the value of the field by the specified amount.
$mul
$mulApplies to: number.
Syntax:
Explanation: Multiplies the value of the field by the specified number.
$min
$minApplies to: number and Date.
Syntax:
Explanation: Only updates the field if the specified value is less than the existing field value.
$max
$maxApplies to: number and Date.
Syntax:
Explanation: Only updates the field if the specified value is more than the existing field value.
Array operators
$addToSet
$addToSetApplies to: Array.
Syntax:
Explanation: Adds elements to an array only if they do not already exist in the set.
$pop
$popApplies to: Array.
Syntax:
Explanation: removes the first or last element of an array. Pass $pop a value of -1 to remove the first element of an array and 1 to remove the last element in an array.
$pull
$pullApplies to: Array.
Syntax:
Explanation: Removes all array elements that match a specified query or value.
$pullAll
$pullAllApplies to: Array.
Syntax:
Explanation: The $pullAll operator removes all instances of the specified values from an existing array.
Unlike the $pull operator that removes elements by specifying a query, $pullAll removes elements that match the listed values.
$push
$pushApplies to: Array.
Syntax:
Explanation: The $push operator appends a specified value to an array.
Array push modifiers
$each
$eachApplies to: Array.
Syntax:
Explanation: Modifies the $push and $addToSet operators to append multiple items for array updates.
$slice
$sliceApplies to: Array.
Syntax:
Explanation: Modifies the $push operator to limit the size of updated arrays.
Must be used with $each modifier. Otherwise it will throw. You can pass an empty array ([ ]) to the $each modifier such that only the $slice modifier has an effect.
$position
$positionApplies to: Array.
Syntax:
Explanation: The $position modifier specifies the location in the array at which the $push operator insert elements. Without the $position modifier, the $push operator inserts elements to the end of the array.
Must be used with $each modifier.
$sort
$sortApplies to: Array.
Syntax:
Explanation: The $sort modifier orders the elements of an array during a $push operation. Pass 1 to sort ascending and -1 to sort descending.
Must be used with $each modifier. Otherwise it will throw. You can pass an empty array ([ ]) to the $each modifier such that only the $sort modifier has an effect.
Last updated
Was this helpful?