Model Methods: Find By Id And Update
Finding a document by id and updating it in the database collection in MerlinDB.
🪶 The findByIdAndUpdate method finds a document by its ID and updates it 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( ) ;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.findByIdAndUpdate(<id>, <update>, <options>); 15 userModel.findByIdAndUpdate( "666cd78dTW96aW1d18a6e137", { 16 $set: { 17 name: "Jack Danger", 18 state: 'Florida' 19 } , 20 $inc: { age: 1 } 21 } , 22 { new: true } 23 //new: true returns the updated documents, default and 'false'; 24 } ) 25 .then( e => console.log( e) ) 26 .catch( e => { 27 console.log( e) ; 28 //returns 29 { 30 acknowledged: true , 31 updatedCount: 0 32 } 33 } )