Reputation: 480
--NodeJS, Mongoose, MongoDB, ExpressJS, EJS--
My website is, there is a login user, where they can submit "name" and "image" of what they want, then the author and other people can comment on that picture. On per each image, i added a function where i count the comments using the .length function which is counting the comments on the picture and projects the number of comments to my ejs file.
here is my schema:
var testSchema = new mongoose.Schema({
name: String,
image: String,
author: {
id: {
type: mongoose.Schema.Types.ObjectId,
ref: "Loginuser"
},
username: String
},
comments: [
{
type: mongoose.Schema.Types.ObjectId,
ref: "Comment"
}
]
});
var commentSchema = new mongoose.Schema ({
author: {
id: {
type: mongoose.Schema.Types.ObjectId,
ref: "Loginuser"
},
username: String
},
text: String,
date: String
});
var loginSchema = new mongoose.Schema ({
username: String,
password: String
});
var TestData = mongoose.model("User", testSchema);
var Comment = mongoose.model("Comment", commentSchema);
var LoginUser = mongoose.model("Loginuser", loginSchema);
I have a function that deletes a comment on User,
app.delete("/index/:id/comments/:comment_id", function(req, res){
Comment.findByIdAndRemove(req.params.comment_id, function(err){
if(err) {
console.log(err);
res.redirect("/index/");
} else {
console.log("Comment successfully deleted!");
res.redirect("back");
}
});
});
Here's some sample data from my mongoDB commentSchema
{ "_id" : ObjectId("57d316e506d8e9186c168a49"),
"text" : "hey baby! why cry?", "
__v" : 0,
"author" : { "id" : ObjectId("57d148acd0f11325b0dcd3e6"),
"username" :
"nagy" },
"date" : "9/10/2016 , 8:07 AM" }
{ "_id" : ObjectId("57d316f706d8e9186c168a4a"),
"text" : "don't you cry baby!",
"__v" : 0,
"author" : { "id" : ObjectId("57d095d727e6b619383a39d0"),
"username": "doge" },
"date" : "9/10/2016 , 8:07 AM" }
{ "_id" : ObjectId("57d3170306d8e9186c168a4b"),
"text" : "wow so cute!!!!!!", "_
_v" : 0,
"author" : { "id" : ObjectId("57d095d727e6b619383a39d0"),
"username" : "doge" }, "date" : "9/10/2016 , 8:07 AM" }
and here's my data on testSchema
{ "_id" : ObjectId("57d316c506d8e9186c168a47"),
"name" : "Baby crying",
"image": "https://s-media-cache-ak0.pinimg.com/564x/d0/bb/ed/d0bbed614353534df9a3be0abe
5f1d78.jpg",
"comments" : [ ObjectId("57d316e506d8e9186c168a49"), ObjectId("57d3
16f706d8e9186c168a4a") ], "author" : { "id" : ObjectId("57d095d727e6b619383a39d0
"), "username" : "doge" }, "__v" : 2 }
{ "_id" : ObjectId("57d316dc06d8e9186c168a48"),
"name" : "Maria?! OZawa?!",
"image" : "https://dncache-mauganscorp.netdna-ssl.com/thumbseg/1092/1092126-bigthumb
nail.jpg",
"comments" : [ ObjectId("57d3170306d8e9186c168a4b") ], "author" : { "
id" : ObjectId("57d148acd0f11325b0dcd3e6"), "username" : "nagy" }, "__v" : 1 }
It is working fine, it's deleting the comment. The problem here is, it is deleting only on the "Comment" model.
I want to delete that same comment also on "TestData" model, because everytime i delete a comment, the count of comments remains the same.
So basically i want to delete that specific comment on both models.
I tried using this approach:
app.delete("/index/:id/comments/:comment_id", function(req, res){
TestData.findByIdAndRemove(req.params.comment_id)function(err){
if(err) {
console.log(err);
res.redirect("/index");
} else {
res.redirect("back");
}
});
});
but it isn't working.
Can you help me on what specific query should i use?
Upvotes: 2
Views: 851
Reputation: 2766
Try the following code:-
TestData.update(
{},
{$pull: {comments: req.params.comment_id}},
{ multi: true },
function(err, data){
console.log(err, data);
});
Hope this will help you.
Upvotes: 0