Model Methods: Find One And Update
Finding and updating a document at the same time in the database collection in MerlinDB.
🪶 The findOneAndUpdate method finds a single document in the collection, updates it according to the specified parameters, and returns the document before or after the update, as configured.
It has 3 parameters, they are:
filter {Object} (Required): Filters what you are looking for;
update {Object}: Document updates;
options {Object}: The query 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.findOneAndUpdate(<query>, <update>, <options>); 15 userModel.findOneAndUpdate( 16 { name: "Maya" } , 17 { 18 $inc: { age: 1 } , 19 //Increase Maya's age by 1 year 20 $set: { name: "Maya Axxwell" } 21 //Update Maya's name to Maya Axxxell 22 } , 23 { new: true } 24 ) .then( e => console.log( e) ) ;