Model Methods: InsertMany
Inserting multiple documents into the database collection in MerlinDB.
🪶 MerlinDB's insertMany method is used to add multiple documents into a collection.
The insert, insertOne, insertMany methods are similar, the only thing that differentiates them is that the 'insertOne' method allows only one document to be inserted into the database, the 'insertMany ' allows multiple documents to be inserted, and 'insert' allows both.
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.insertMany(<data(Array)>, <options>); 15 userModel.insertMany( [ 16 { name: "Isabelle", age: 26 } , 17 { name: "Chris", age: 27 } , 18 ], { ordered: true } ) 19 //Currently it provides the option to order, default is true; 20 //It orders the data in the database and returns the $order in queries 21 //By default all data is (ordered: true) ordered by timestamp when entered. 22 .then( e => { 23 console.log( e) ; 24 //returns 25 { 26 acknowledged: true , 27 insertedI: "4As32...", 28 data: [ 29 { name: "Isabelle", age: 26 , id_: "4As32..." } , 30 { name: "Chris", age: 27 , id_: "Zr3aG..." } , 31 ] 32 } 33 } ) .catch( e => { 34 console.log( e) ; 35 //Error message... 36 } )