0% found this document useful (0 votes)
26 views9 pages

Var Mongoose

The document defines a Blog schema and model using Mongoose to interact with a MongoDB database. It inserts sample blog documents, then demonstrates various query and update methods including find, delete, update, replace operations on the Blog model and documents. These include findOne, findById, deleteOne, deleteMany, updateOne, replaceOne and more. The operations select specific fields, and console.log results including deleted/modified counts.

Uploaded by

Ayeza Baseer
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
26 views9 pages

Var Mongoose

The document defines a Blog schema and model using Mongoose to interact with a MongoDB database. It inserts sample blog documents, then demonstrates various query and update methods including find, delete, update, replace operations on the Blog model and documents. These include findOne, findById, deleteOne, deleteMany, updateOne, replaceOne and more. The operations select specific fields, and console.log results including deleted/modified counts.

Uploaded by

Ayeza Baseer
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 9

var mongoose = require('mongoose');

// mongoose.connect('mongodb://localhost:27017/mBusiness_DB', function (err) {


var url="mongodb://localhost:27017/mBusiness_DB";
const blogDataSchema=new mongoose.Schema({
blogId:{
type:String,
required:true,
unique:true
},
title:{
type:String,
required:true
},
author:{
type:String,
required:true
},
dateTime:{
type:Date,
default:Date.now
},
blogRating:[{votes:Number, favourites:Number}],
comments:[{body:String,date:Date}]
});
const Blog=mongoose.model('Blog',blogDataSchema);
const blogs=[
{
blogId:"12A",
title:"Blog1",
author:"Ayeza",
blogRating:[{
votes:1,
favourites:1
}],
comments:[{
body:"good",
date:"2021-10-30"

}]}
,
{
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);

console.log(blog) //Blog 2 Blog5


mongoose.connection.close()

})
//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()

})

//*******11findOneAndReplace */ Replace the first document that matches this


filter Finds a matching document, replaces it with the provided doc, and passes
the returned doc to the callback.

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;

console.log(blog) //{acknowlwdged:true, modifiedCount:1s}


mongoose.connection.close()

})
})

You might also like