Model Methods: Update Many

Finding multiple documents and updating them in the database collection in MerlinDB.

🪶 The updateMany method finds multiple documents that match the specified criteria and updates all documents that match those criteria with the new data provided.

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(
  • "USER-DATABASE"
  • )
  • ;
    6
    7
  • const
  • UserSchema =
  • Schema(
  • {
  •   
    8  
  • name:
  • String
  • ,
    9  
  • age:
  • Number
  • 10
  • }
  • )
  • ;
    11
    12
  • var
  • userModel = merlin
  • .model(
  • "User", UserSchema
  • )
  • ;
    13
    14
  • //userModel.updateMany(<query>, <update>, <options>);
  • 15userModel
  • .updateMany(
  • 16  
  • {
  • name:
  • {
  • $regex:
  • "Samantha"
  • }
  • }
  • ,
    17  
  • {
  • $set:
  • {
  • name:
  • "Samantha Smith"
  • }
  • }
  • ,
    18  
  • {
  • 19      
  • new:
  • true
  • ,
    20    
  • //returns the updated document;
  • 21      
  • upsert:
  • true
  • 22      
  • //Create a new document if it does not exist;
  • 23  
  • }
  • 24
  • )
  • .then(
  • e => console
  • .log(
  • e
  • )
  • )
  • ;