Mongoose mongodb,删除数组上的元素
--NodeJS、Mongoose、MongoDB、ExpressJS、EJS-- 我的网站是,有一个登录用户,在那里他们可以提交他们想要的“名字”和“图片”,然后作者和其他人可以对图片发表评论。在每张图片上,我添加了一个函数,我使用.length函数计算图片上的注释,并将注释数投影到我的ejs文件中 这是我的模式:Mongoose mongodb,删除数组上的元素,mongoose,Mongoose,--NodeJS、Mongoose、MongoDB、ExpressJS、EJS-- 我的网站是,有一个登录用户,在那里他们可以提交他们想要的“名字”和“图片”,然后作者和其他人可以对图片发表评论。在每张图片上,我添加了一个函数,我使用.length函数计算图片上的注释,并将注释数投影到我的ejs文件中 这是我的模式: var testSchema = new mongoose.Schema({ name: String, image: String, author: {
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);
我有一个删除用户评论的功能
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");
}
});
});
下面是我的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" }
{ "_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 }
下面是我在testSchema
{ "_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" }
{ "_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 }
如您所见,testSchema和commentSchema上的comment的objectId是相同的。基本上他们是相关的
它工作正常,正在删除评论。这里的问题是,它只是在“Comment”模型上删除
我还想在“TestData”模型上删除相同的注释,因为每次删除注释时,注释的数量保持不变
我想删除这两个模型的具体评论
我尝试使用这种方法:
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");
}
});
});
但它不起作用
你能帮助我使用什么具体的查询吗
更新:我添加了一个预中间件,但仍然不能工作,也许我做错了。我已将预中间件放在“app.delete”上方。对吗
commentSchema.pre('remove', function (next) {
User.update(
{ comments: this },
{ $pull: { comments: this._id } },
{ multi: true }
).exec(next)
});
app.delete('/index/:id/comments/:comment_id', function (req, res) {
Comment.findById(req.params.comment_id, function(comment){
comment.remove();
console.log("comment succesfully deleted!");
res.redirect("back");
});
});
在通读了stackoverflow这个与我类似的问题后,我也尝试了这个。。。类似的预中间件,但它使用“更新”
是的,仍然无法工作:(您需要手动从数组中删除注释id。更好的方法是向注释架构中添加注释:
commentSchema.pre('remove',函数(下一步){
User.update(
{评论:本},
{$pull:{comments:this.\u id}},
{multi:true}
).exec(下一个)
})
但是删除中间件只能通过调用doc.remove
来执行,而不是Model.findbyiandremove
。因此,您需要稍微更改代码(我在这里使用Promissions以提高可读性,但您可以通过回调来实现这一点):
app.delete('/index/:id/comments/:comment_id',函数(req,res){
Comment.findById(请求参数Comment\u id)
.然后(功能(注释){
return comment.remove()//doc.remove将触发remove中间件
})
.然后(函数(){
console.log('注释已成功删除!')
return res.redirect('back')
})
.catch(函数(err){
console.log(错误)
res.redirect(“/index”)
})
})
我试图将此粘贴到代码上,但它给了我一个“意外令牌”错误。我将您的代码编辑为此..commentSchema.pre('remove',function(next){User.update({comments:this},{$pull:{comments:this.\u id},{multi:true}).exec(next)});app.delete('/index/:id/comments/:comments/:comments\u id',function(req,res){Comment.findById(req.params.Comment_id,function(err){if(err){console.log(err);}else{return Comment.remove();console.log(“Comment successfully deleted!”;res.redirect(“back”);}}});
错误是“Comment”没有定义在“Comment.remove()”app.delete('/index/:id/comments/:comment_id',function(req,res){comment.findById(req.params.comment_id,function(err){if(err){console.log(err)}else{return comment.remove();console.log(“comment successfully deleted!”;res.redirect(“back”);}});
comment.remove()上没有定义“comment”…谢谢。我如何修复它?app.delete('/index/:id/comments/:comment_id',function(req,res){comment.findById(req.params.comment_id,function(comment){comment.remove();console.log(“comment successfully deleted!”);res.redirect(“back”);};
我更新了代码,忘了函数(comment)。但是现在错误给了我“无法读取null的属性'remove'”很抱歉。Model.findById
如果找不到文档,可能会返回null值。您必须在comment.remove()
之前验证它是否存在。它很简单,就像if(comment){comment.remove()}否则{//返回404 error}