Update Documents
To update documents call update model's method with 2 arguments: Filter Query and partial document with updated states (or specific objects that describes an update strategy for the field)
await userModel.update(
[
{
name: {
$eq: 'John'
}
}
],
{
isActive: false
}
);
Specific objects that describes an update strategy for the field depending on its type:
- A field takes one operator per update.
{ $add: 1, $multiply: 2 } is refused: send two updates. - A pointer (
'$name') as a value copies the value of another property of the same document. It has to name a property of the same type. A document in which that property has no value keeps its own value. In a STRING, text that starts with $ and names no property ('$5 off') is stored as text. - An update changes every document the filter matches. It can't give one value of a
unique property to several documents.
String Update Query
| Operator | Type | Effect |
|---|
$concat | Requires object with delimiter property that specify concatenation delimiter and parts property that is an array of strings (pointers like '$name' will be evaluated) | Appends a string to the current value |
$replace | [string, string, string] | Replaces substring using [search, replacement, flags] format. search is a regular expression, used as it is written ('\\.' for a dot); flags is the combination of RegExp flags, e.g. 'gm', or ''. An invalid expression or invalid flags is an error |
Number Update Query
Supported update operators
| Operator | Type | Effect |
|---|
$add | number | Adds a value to the current number |
$subtract | number | Subtracts a value from the current number |
$multiply | number | Multiplies the current number by a value |
$divide | number | Divides the current number by a value |
$round | number | Rounds the current number using provided precision |
Array Update Query
Supported update operators
| Operator | Type | Effect |
|---|
$addItem | any | Appends an element to the array, also when the array holds the value already |
$removeItem | any | Removes every element with the value |
Time Update Query
Some specific contracts to deal with TIME field type:
UnitOfTime enum
| Enum key | Literal value | Description |
|---|
| YEAR | "year" | Calendar year |
| MONTH | "month" | Calendar month |
| WEEK | "week" | 7-day period |
| DAY | "day" | Day of month |
| HOUR | "hour" | Hour of day |
| MINUTE | "minute" | Minute of hour |
| SECOND | "second" | Second of minute |
| MILLISECOND | "millisecond" | Thousandth of a second |
Weekday enum
| Enum key | Value (update) | Value (find) |
|---|
| SUNDAY | Sunday | 0 |
| MONDAY | Monday | 1 |
| TUESDAY | Tuesday | 2 |
| WEDNESDAY | Wednesday | 3 |
| THURSDAY | Thursday | 4 |
| FRIDAY | Friday | 5 |
| SATURDAY | Saturday | 6 |
Supported update operators for the TIME fields:
| Operator | Structure or type | Effect |
|---|
$add | { amount, unit } | Adds time (amount × unit) |
$subtract | { amount, unit } | Subtracts time |
$toStartOf | UnitOfTime | Rounds down to start of given unit, in the server's time zone; a week starts on Monday |
$toEndOf | UnitOfTime | Rounds up to end of unit |
$toNextWeekday | Weekday | Jumps forward to next weekday (Sunday -> next Sunday) |
$toLastWeekday | Weekday | Jumps back to previous weekday (Sunday -> previous Sunday) |