Model Methods: Find One And Replace
Finding and replacing a document at the same time in the database collection in MerlinDB.
🪶 The findOneAndReplace method finds a single document in the collection, replaces it with a new provided document, and returns the document before or after the replacement, as configured.
It has 3 parameters, they are:
filter {Object} (Required): Filters what you are looking for;
replace {Object}: Document updates;
options {Object}: The options;
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( ) ;5 merlin.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.findOneAndReplace(<query>, <replace>, <options>); 15 userModel.findOneAndReplace( 16 { name: "Maya" } , 17 { name: "Maya Axxwell", age: -9 , city: "Miami" } , 18 { 19 //If true inserts a document if it does not exist 20 upsert: false , 21 //Before returns the 'old' document, after returns the 'new' document 22 returnDocument: "before" //or "after" 23 } 24 ) .then( e => console.log( e) ) ;