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

OperatorTypeEffect
$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
OperatorTypeEffect
$addnumberAdds a value to the current number
$subtractnumberSubtracts a value from the current number
$multiplynumberMultiplies the current number by a value
$dividenumberDivides the current number by a value
$roundnumberRounds the current number using provided precision

Array Update Query

Supported update operators
OperatorTypeEffect
$addItemanyAppends an element to the array, also when the array holds the value already
$removeItemanyRemoves every element with the value

Time Update Query

Some specific contracts to deal with TIME field type:
UnitOfTime enum
Enum keyLiteral valueDescription
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 keyValue (update)Value (find)
SUNDAYSunday0
MONDAYMonday1
TUESDAYTuesday2
WEDNESDAYWednesday3
THURSDAYThursday4
FRIDAYFriday5
SATURDAYSaturday6

Supported update operators for the TIME fields:

OperatorStructure or typeEffect
$add{ amount, unit }Adds time (amount × unit)
$subtract{ amount, unit }Subtracts time
$toStartOfUnitOfTimeRounds down to start of given unit, in the server's time zone; a week starts on Monday
$toEndOfUnitOfTimeRounds up to end of unit
$toNextWeekdayWeekdayJumps forward to next weekday (Sunday -> next Sunday)
$toLastWeekdayWeekdayJumps back to previous weekday (Sunday -> previous Sunday)