Var Mongoose
Var Mongoose
}]}
,
{
blogId:"12B",
title:"Blog2",
author:"Ayeza",
blogRating:[{
votes:1,
favourites:1
}],
comments:[{
body:"hi",
date:"2021-10-30"
}]
},{
blogId:"12C",
title:"Blog3",
author:"Ayesha",
blogRating:[{
votes:2,
favourites:2
}],
comments:[{
body:"hi",
date:"2021-10-30"
}]}
,
{
blogId:"12D",
title:"Blog4",
author:"Ayesha",
blogRating:[{
votes:1,
favourites:1
}],
comments:[{
body:"hi",
date:"2021-10-31"
}]
},
{
blogId:"12E",
title:"Blog5",
author:"Ayeza",
blogRating:[{
votes:1,
favourites:1
}],
comments:[{
body:"good",
date:"2021-10-30"
}]},
{
blogId:"12F",
title:"Blog6",
author:"Ayeza",
blogRating:[{
votes:1,
favourites:1
}],
comments:[{
body:"good",
date:"2021-10-30"
}]},
{
blogId:"12G",
title:"Blog7",
author:"Ayeza",
blogRating:[{
votes:1,
favourites:1
}],
comments:[{
body:"good",
date:"2021-10-30"
}]},
{
blogId:"12H",
title:"Blog8",
author:"Ayeza",
blogRating:[{
votes:1,
favourites:1
}],
comments:[{
body:"good",
date:"2021-10-30"
}]},
{
blogId:"12I",
title:"Blog9",
author:"Ayeza",
blogRating:[{
votes:1,
favourites:1
}],
comments:[{
body:"good",
date:"2021-10-30"
}]},
{
blogId:"12J",
title:"Blog10",
author:"Ayeza",
blogRating:[{
votes:1,
favourites:1
}],
comments:[{
body:"good",
date:"2021-10-30"
}]}
];
mongoose.connect(url,function(err){
Blog.insertMany(blogs,function(err){
if(err) return handleError(err);
console.log("saved")
mongoose.connection.close()
})
//1********deleteMany********** */
//The following operation deletes all documents where author is ayesha It
returns an object with the property deletedCount containing the number of
documents deleted
Blog.deleteMany({
'author':'Ayesha'
})
.select({title:1, blogRating:1})
.exec(function(err,blog){
if(err) return handleError(err);
//deleted count
console.log(blog) //2
mongoose.connection.close()
})
//2*********deleteOne********* */
//Deletes the first document that matches conditions from the collection. It
returns an object with the property deletedCount indicating how many documents
were deleted.
Blog.deleteOne({
'blogRating.votes':'1'
})
.select({title:1, blogRating:1})
.exec(function(err,blog){
if(err) return handleError(err);
//deleted count
console.log(blog) //1
mongoose.connection.close()
})
//3********find*********** */
Blog.find({
'author':'Ayeza', 'blogRating.votes':'1'
})
.select({title:1, blogRating:1})
.exec(function(err,blog){
if(err) return handleError(err);
})
//4*******findByidcan give only one argument that is id */
Blog.findById({
_id: "617e477e103a6d35b71a74b0"
})
.select({title:1, blogRating:1})
.exec(function(err,blog){
if(err) return err;
console.log(blog)
mongoose.connection.close()
})
//5*******findByIdAndDelete returnsobjectdeleted*****
Blog.findByIdAndDelete({
_id: "617e477e103a6d35b71a74b0"
})
.select({title:1, blogRating:1})
.exec(function(err,blog){
if(err) return err;
console.log(blog)
mongoose.connection.close()
})
// ****6findByIdAndRemove */
Blog.findByIdAndRemove({
_id: "617e62bccc76eb055269ef22"
})
.select({title:1, blogRating:1})
.exec(function(err,blog){
if(err) return err;
console.log(blog)
mongoose.connection.close()
})
//****8findByIdAndUpdate */
Blog.findByIdAndUpdate(
"617e7f8a95701ada3f8b5b26" , {author:'Kashaf'}
,function(err,blog){
if(err) return err;
console.log(blog)
mongoose.connection.close()
})
//****findOne */
Blog.findOne({
'author':'Ayeza'
})
.select({title:1, blogRating:1})
.exec(function(err,blog){
if(err) return err;
console.log(blog)
mongoose.connection.close()
})
//*****9findOneAnddelete */
Blog.findOneAndDelete({
'author':'Ayeza'
})
.select({title:1, blogRating:1})
.exec(function(err,blog){
if(err) return err;
console.log(blog)
mongoose.connection.close()
})
//*******10findOneAndRemove */
Blog.findOneAndRemove({
'author':'Kashaf'
})
.select({title:1, blogRating:1})
.exec(function(err,blog){
if(err) return err;
console.log(blog)
mongoose.connection.close()
})
var newDoc={
blogId:"12L",
title:"Blog15",
author:"Kashaf",
blogRating:[{
votes:1,
favourites:1
}],
comments:[{
body:"good",
date:"2021-10-30"
}]
}
//11
Blog.findOneAndReplace({
'author':'Ayeza'
},newDoc)
.select({title:1, blogRating:1})
.exec(function(err,blog){
if(err) return err;
console.log(blog)
mongoose.connection.close()
})
// //*******12findOneAndUpdate****** */
Blog.findOneAndUpdate({
'author':'Kashaf'
},{'author':'Ayeza'})
.select({title:1, blogRating:1})
.exec(function(err,blog){
if(err) return err;
console.log(blog)
mongoose.connection.close()
})
//*********13replaceOne */
Blog.replaceOne({
'author':'Ayeza'
},newDoc)
.select({title:1, blogRating:1})
.exec(function(err,blog){
if(err) return err;
console.log(blog)
mongoose.connection.close()
})
//**********updateOne */
Blog.updateOne({
'author':'Kashaf'
},{'author':'Ayeza'})
.select({title:1, blogRating:1})
.exec(function(err,blog){
if(err) return err;
})
})