Create and insert document using Express in mongoDb

Insert single document
const mongoose = require('mongoose');
// connection creation
mongoose.connect("mongodb://localhost:27017/newDB",{useNewUrlParser: true})
// use promises
.then(()=>{
    console.log("connction successful...")
})
.catch((err)=>console.log(err));



const playlistSchema = new mongoose.Schema ({
    // define schema
    name:{
        type : String,
        required : true
    },
    sirname : String,
    email : String,
    role : String,
    active : Boolean,
    date : {
        type: Date,
        default : Date.now
    }
})
// collection creation
// in model("collection name", define schema)
const PlayList = new mongoose.model("PlayList",playlistSchema)

// create document or insert

const reactPlaylist = new PlayList(
    {
      name:"Shreyash",
      sirname :  "KOlhe",
      email : "shreyashkolhe2001@gmail.com",
      role : "front-end",
      active : true,
    }
 )
    
    //  The save() function is used to save the document to the database. 
 reactPlaylist.save();



In Another way to .Save() function used using async or promises. it help to return error or give message to inserting data.

1. using Async
const createDocument = async ()=>{
    try{
        const reactPlaylist = new PlayList(
            {
              name:"Shreyash",
              sirname :  "KOlhe",
              email : "shreyashkolhe2001@gmail.com",
              role : "front-end",
              active : true,
            })
            const result = await reactPlaylist.save();
            console.log("Data inserted successfully!");
            console.log(result);
           
    }catch(err){
        console.log(err)
    }
}

createDocument();
Data inserted successfully!
{
  name: 'Shreyash',
  sirname: 'KOlhe',
  email: 'shreyashkolhe2001@gmail.com',
  role: 'front-end',
  active: true,
  _id: new ObjectId("61cbf855aba034e3e6c54b5e"),
  date: 2021-12-29T05:55:33.949Z,
  __v: 0
}
2. Using Promises
reactPlaylist.save()
    .then((result)=>{
        console.log("Data inserted successfully!");
         console.log(result)
 })
    .catch((err)=>console.log(err));
Data inserted successfully!
{
  name: 'Shreyash',
  sirname: 'KOlhe',
  email: 'shreyashkolhe2001@gmail.com',
  role: 'front-end',
  active: true,
  _id: new ObjectId("61cbf855aba034e3e6c54b5e"),
  date: 2021-12-29T05:55:33.949Z,
  __v: 0
}
Insert Multiple documents
const mongoose = require('mongoose');
mongoose.connect("mongodb://localhost:27017/newDB",{useNewUrlParser: true})
.then(()=>{
    console.log("connction successful...")
})
.catch((err)=>console.log(err));

const playlistSchema = new mongoose.Schema ({
    // define schema
    name:{
        type : String,
        required : true
    },
    sirname : String,
    email : String,
    role : String,
    active : Boolean,
    date : {
        type: Date,
        default : Date.now
    }
})

// collection creation
// in model("collection name", define schema)
const PlayList = new mongoose.model("PlayList",playlistSchema)

// create document or insert

//  The save() function is used to save the document to the database.
const createDocument = async ()=>{
    try{
        const FirstStudent = new PlayList(
            {
              name:"Shreyash",
              sirname :  "KOlhe",
              email : "shreyashkolhe2001@gmail.com",
              role : "front-end",
              active : true,
            })

            const SecoundStudent = new PlayList(
                {
                  name:"Vaibhav",
                  sirname :  "Bandal",
                  email : "vaibhav@gmail.com",
                  role : "front-end",
                  active : true,
            })
           
            const ThirdStudent = new PlayList(
               {
                 name:"Omkar",
                 sirname :  "patke",
                 email : "omkar@gmail.com",
                 role : "front-end",
                 active : true,
                })
               
            const ForthStudent = new PlayList(
                {
                  name:"amit",
                  sirname :  "badhe",
                  email : "amit@gmail.com",
                  role : "front-end",
                  active : true,
                })
           
           
            const result = await PlayList.insertMany([FirstStudent, SecoundStudent, ThirdStudent, ForthStudent]);
            console.log("Data inserted successfully!");
            console.log(result);
           
    }catch(err){
        console.log(err)
    }
}

createDocument();
PS D:\connect mogodb node\mongus> node src/app.js
connction successful...
Data inserted successfully!
[
  {
    name: 'Shreyash',
    sirname: 'KOlhe',
    email: 'shreyashkolhe2001@gmail.com',
    role: 'front-end',
    active: true,
    _id: new ObjectId("61cc0946c62a51adf6bc23d9"),
    date: 2021-12-29T07:07:50.874Z,
    __v: 0
  },
  {
    name: 'Vaibhav',
    sirname: 'Bandal',
    email: 'vaibhav@gmail.com',
    role: 'front-end',
    active: true,
    _id: new ObjectId("61cc0946c62a51adf6bc23da"),
    date: 2021-12-29T07:07:50.876Z,
    __v: 0
  },
  {
    name: 'Omkar',
    sirname: 'patke',
    email: 'omkar@gmail.com',
    role: 'front-end',
    active: true,
    _id: new ObjectId("61cc0946c62a51adf6bc23db"),
    date: 2021-12-29T07:07:50.877Z,
    __v: 0
  },
  {
    name: 'amit',
    sirname: 'badhe',
    email: 'amit@gmail.com',
    role: 'front-end',
    active: true,
    _id: new ObjectId("61cc0946c62a51adf6bc23dc"),
    date: 2021-12-29T07:07:50.878Z,
    __v: 0
  }
]