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)
// get all data inside Database
const getDocument = async()=>{
const result = await PlayList.find();
console.log(result);
}
getDocument();
connction successful...
[
{
_id: new ObjectId("61cbf6b1119213b1dfba2431"),
name: 'Shreyash',
sirname: 'KOlhe',
email: 'shreyashkolhe2001@gmail.com',
role: 'front-end',
active: true,
date: 2021-12-29T05:48:33.350Z,
__v: 0
},
{
_id: new ObjectId("61cc0946c62a51adf6bc23d9"),
name: 'Shreyash',
sirname: 'KOlhe',
email: 'shreyashkolhe2001@gmail.com',
role: 'front-end',
active: true,
date: 2021-12-29T07:07:50.874Z,
__v: 0
},
{
_id: new ObjectId("61cc0946c62a51adf6bc23da"),
name: 'Vaibhav',
sirname: 'Bandal',
email: 'vaibhav@gmail.com',
role: 'front-end',
active: true,
date: 2021-12-29T07:07:50.876Z,
__v: 0
},
{
_id: new ObjectId("61cc0946c62a51adf6bc23db"),
name: 'Omkar',
sirname: 'patke',
email: 'omkar@gmail.com',
role: 'front-end',
active: true,
date: 2021-12-29T07:07:50.877Z,
__v: 0
},
{
_id: new ObjectId("61cc0946c62a51adf6bc23dc"),
name: 'amit',
sirname: 'badhe',
email: 'amit@gmail.com',
role: 'front-end',
active: true,
date: 2021-12-29T07:07:50.878Z,
__v: 0
}
]
Pass query and read document
const getDocument = async()=>{
const result = await PlayList.find({name: "Shreyash"});
console.log(result);
}
getDocument();
connction successful...
[
{
_id: new ObjectId("61cbf6b1119213b1dfba2431"),
name: 'Shreyash',
sirname: 'KOlhe',
email: 'shreyashkolhe2001@gmail.com',
role: 'front-end',
active: true,
date: 2021-12-29T05:48:33.350Z,
__v: 0
},
{
_id: new ObjectId("61cc0946c62a51adf6bc23d9"),
name: 'Shreyash',
sirname: 'KOlhe',
email: 'shreyashkolhe2001@gmail.com',
role: 'front-end',
active: true,
date: 2021-12-29T07:07:50.874Z,
__v: 0
}
]
Read/Get one field
const getDocument = async()=>{
const result = await PlayList.find({name: "Shreyash"})
.select({role:1});
console.log(result);
}
getDocument();
connction successful...
[
{ _id: new ObjectId("61cbf6b1119213b1dfba2431"), role: 'front-end' },
{ _id: new ObjectId("61cc0946c62a51adf6bc23d9"), role: 'front-end' }
]
Read/Get one field in only one document
const getDocument = async()=>{
const result = await PlayList.find({name: "Shreyash"})
.select({role:1})
.limit(1);
console.log(result);
}
getDocument();
connction successful...
[
{ _id: new ObjectId("61cbf6b1119213b1dfba2431"), role: 'front-end' }
]
Using Async Method
const getDocument = async()=>{
try {
const result = await PlayList.find({name: "Shreyash"})
.select({role:1})
.limit(1);
console.log(result);
}catch(err){
console.log(err);
}
}
getDocument();
Post a Comment