如何在mongodb中使用node.js删除嵌入(嵌套)文档
这是我的模式…如何在mongodb中使用node.js删除嵌入(嵌套)文档,node.js,mongodb,Node.js,Mongodb,这是我的模式… const commentSchema = new mongoose.Schema({ comment: String, imagename:String }); const Comment = new mongoose.model("Comment",commentSchema); const userSchema = new mongoose.Schema({ email: String, p
const commentSchema = new mongoose.Schema({
comment: String,
imagename:String
});
const Comment = new mongoose.model("Comment",commentSchema);
const userSchema = new mongoose.Schema({
email: String,
password:String,
comments: [commentSchema]
});
userSchema.plugin(passportLocalMongoose);
const User = new mongoose.model("User", userSchema);
app.post("/delete", function(req,res){
if(req.isAuthenticated()) {
User.findById(req.user.id,function(err, foundUser){
if(err){
console.log(err);
}
else{
const uid = foundUser.id; //this is the User iD
const checkedItemId = (req.body.checkbox); //this is the comment ID
console.log(checkedItemId);
User.updateOne(_id:"uid","comments._id": checkedItemId},{$pull :{comments:{_id :checkedItemId}}});
User.Comment.pull(checkedItemId);
if(!err){
console.log("successfully deleted");
res.redirect("data")
}
}
});
}
else{
res.redirect("/");
}
});
"_id" : ObjectId("5eb798bb64d8f94df08a6ae7"),
"username" : "random",
"comments" : [
{
"_id" : ObjectId("5eb798cd64d8f94df08a6ae8"),
"comment" : "hello",
"imagename" : "image-1589090509389.jpeg"
}
]
EDIT: After this question I got to know that there are 2 ways to solve this question, one is embedded and the other is a reference.
previously all the answers were dependent on the embedded model in the schema, and this question works on the reference model.
我正在尝试使用…删除嵌入的文档。
const commentSchema = new mongoose.Schema({
comment: String,
imagename:String
});
const Comment = new mongoose.model("Comment",commentSchema);
const userSchema = new mongoose.Schema({
email: String,
password:String,
comments: [commentSchema]
});
userSchema.plugin(passportLocalMongoose);
const User = new mongoose.model("User", userSchema);
app.post("/delete", function(req,res){
if(req.isAuthenticated()) {
User.findById(req.user.id,function(err, foundUser){
if(err){
console.log(err);
}
else{
const uid = foundUser.id; //this is the User iD
const checkedItemId = (req.body.checkbox); //this is the comment ID
console.log(checkedItemId);
User.updateOne(_id:"uid","comments._id": checkedItemId},{$pull :{comments:{_id :checkedItemId}}});
User.Comment.pull(checkedItemId);
if(!err){
console.log("successfully deleted");
res.redirect("data")
}
}
});
}
else{
res.redirect("/");
}
});
"_id" : ObjectId("5eb798bb64d8f94df08a6ae7"),
"username" : "random",
"comments" : [
{
"_id" : ObjectId("5eb798cd64d8f94df08a6ae8"),
"comment" : "hello",
"imagename" : "image-1589090509389.jpeg"
}
]
EDIT: After this question I got to know that there are 2 ways to solve this question, one is embedded and the other is a reference.
previously all the answers were dependent on the embedded model in the schema, and this question works on the reference model.
>我想删除一条评论:hello和相关图像。
const commentSchema = new mongoose.Schema({
comment: String,
imagename:String
});
const Comment = new mongoose.model("Comment",commentSchema);
const userSchema = new mongoose.Schema({
email: String,
password:String,
comments: [commentSchema]
});
userSchema.plugin(passportLocalMongoose);
const User = new mongoose.model("User", userSchema);
app.post("/delete", function(req,res){
if(req.isAuthenticated()) {
User.findById(req.user.id,function(err, foundUser){
if(err){
console.log(err);
}
else{
const uid = foundUser.id; //this is the User iD
const checkedItemId = (req.body.checkbox); //this is the comment ID
console.log(checkedItemId);
User.updateOne(_id:"uid","comments._id": checkedItemId},{$pull :{comments:{_id :checkedItemId}}});
User.Comment.pull(checkedItemId);
if(!err){
console.log("successfully deleted");
res.redirect("data")
}
}
});
}
else{
res.redirect("/");
}
});
"_id" : ObjectId("5eb798bb64d8f94df08a6ae7"),
"username" : "random",
"comments" : [
{
"_id" : ObjectId("5eb798cd64d8f94df08a6ae8"),
"comment" : "hello",
"imagename" : "image-1589090509389.jpeg"
}
]
EDIT: After this question I got to know that there are 2 ways to solve this question, one is embedded and the other is a reference.
previously all the answers were dependent on the embedded model in the schema, and this question works on the reference model.
当用户单击ejs页面上的按钮时,我试图删除用户中选定的注释。
它自动生成注释id,用户id使用身份验证生成。
因此,使用注释id和用户id,如何使用node.js删除MongoDB中的注释。我尝试了updateOne和pull,但不起作用。
const commentSchema = new mongoose.Schema({
comment: String,
imagename:String
});
const Comment = new mongoose.model("Comment",commentSchema);
const userSchema = new mongoose.Schema({
email: String,
password:String,
comments: [commentSchema]
});
userSchema.plugin(passportLocalMongoose);
const User = new mongoose.model("User", userSchema);
app.post("/delete", function(req,res){
if(req.isAuthenticated()) {
User.findById(req.user.id,function(err, foundUser){
if(err){
console.log(err);
}
else{
const uid = foundUser.id; //this is the User iD
const checkedItemId = (req.body.checkbox); //this is the comment ID
console.log(checkedItemId);
User.updateOne(_id:"uid","comments._id": checkedItemId},{$pull :{comments:{_id :checkedItemId}}});
User.Comment.pull(checkedItemId);
if(!err){
console.log("successfully deleted");
res.redirect("data")
}
}
});
}
else{
res.redirect("/");
}
});
"_id" : ObjectId("5eb798bb64d8f94df08a6ae7"),
"username" : "random",
"comments" : [
{
"_id" : ObjectId("5eb798cd64d8f94df08a6ae8"),
"comment" : "hello",
"imagename" : "image-1589090509389.jpeg"
}
]
EDIT: After this question I got to know that there are 2 ways to solve this question, one is embedded and the other is a reference.
previously all the answers were dependent on the embedded model in the schema, and this question works on the reference model.
您正在查询中以字符串形式传递注释id和用户id,这将不起作用,您需要将其转换为ObjectId,请检查下面的更新代码
var mongoose = require('mongoose');
app.post("/delete", function(req,res){
if(req.isAuthenticated()) {
User.findById(req.user.id,function(err, foundUser){
if(err){
console.log(err);
}
else{
const uid = foundUser.id; //this is the User iD
const checkedItemId = mongoose.Types.ObjectId(req.body.checkbox); //this is the comment ID
console.log(checkedItemId);
User.updateOne({_id: uid},{$pull :{comments:{_id :checkedItemId}}}, function(err, results){
if(!err){
console.log("successfully deleted");
res.redirect("data")
} else {
console.log("error in deletion");
res.redirect("/");
}
});
}
});
} else {
res.redirect("/");
}
});
在上面的代码中,您正在从findbyId函数中获取ObjecId格式的用户id,只需不在其上添加“”,并将注释id转换为OBJEID格式如果下面的答案之一回答了您的问题,按照此网站的工作方式,您将“接受”并“投票”答案,更多信息:。但前提是你的问题真的得到了回答。如果不是,考虑在问题中增加更多的细节,这应该回答你的问题,确保你在提问之前进行搜索。这能回答你的问题吗?谢谢你的反馈!声誉低于15的人所投的票会被记录下来,但不会改变公开显示的帖子分数。@Puneethingh。我已经检查了你的回复@aldokkani,但我猜他已经使用了引用方法,我在嵌入方法上的代码世界,以前不知道mongoose.Types.ObjectId(req.body.checkbox)。嗨,bhai,我想把你搜遍我怀疑你是否能找到答案。。User.findById这是一个额外的调用,您可以在不使用“User.findById”的情况下执行相同的调用。。在内部查询mongoose.Types.ObjectId(req.user.id)中使用此选项