Operators: $set

Updating values ​​of a field in a document.

🪶 The $set operator is used to update the value of a specific
  • field in a document.

    NOTE: You can only use the $set, $inc operators in document update methods such as the update, modify and replace methods .


    Database: Client > model: Users

    Here we will simulate a database with the Users collection:
    1
  • COLLECTION:
  • Users > [
    2  
  • {
  • name:
  • "Chris Axxwell",
  • age:
  • 27
  • ,
  • pets:
  • 'cat'  
  • }
  • ,
    3  
  • {
  • name:
  • "Chris Buckman",
  • age:
  • 22
  • ,
  • pets:
  • 'cat'
  • }
  • ,
    4  
  • {
  • name:
  • "Santiago Smith",
  • age:
  • 26
  • ,
  • pets:
  • 'dog'
  • }
  • ,
    5   ...
    6]

    Query the above database:

    1
  • import
  • MerlinDB,
  • {
  • Schema
  • }
  • from
  • "@chrisaxxwell/merlin-db";
    2
  • //Or if you are using 'merlindb.max.js' just call 'new MerlinDB()';
  • 3
    4
  • const
  • merlin =
  • new
  • MerlinDB(
  • )
  • ;
    5merlin
  • .connect(
  • "Client"
  • )
  • ;
    6
  • var
  • clientModel = merlin
  • .model(
  • "Users", <userSchema>
  • )
  • ;
    7
    8
  • //$set: <object>;
  • 9clientModel
  • .updateOne(
  • 10  
  • {
  • name:
  • {
  • $regex:
  • "Chris Axxwell"
  • }
  • }
  • ,
    11  
  • {
  • $set:
  • {
  • name:
  • "Christopher Axxwell",
  • age:
  • 27
  • .
  • 4
  • }
  • }
  • 12
  • )
  • .then(
  • e => console
  • .log(
  • e
  • )
  • )
  • ;
    13
    14______________________________
    15
    16
  • COLLECTION:
  • Users > [
    17  
  • {
  • name:
  • "Christopher Axxwell",
  • age:
  • 27
  • .
  • 4
  • ,
  • pets:
  • 'cat'  
  • }
  • ,
    18  
  • {
  • name:
  • "Chris Buckman",
  • age:
  • 22
  • ,
  • pets:
  • 'cat'
  • }
  • ,
    19  
  • {
  • name:
  • "Santiago Smith",
  • age:
  • 26
  • ,
  • pets:
  • 'dog'
  • }
  • ,
    20   ...
    21]