【问题标题】:Mongoose removing a specific comment in an array that is attached to UserMongoose 删除附加到用户的数组中的特定评论
【发布时间】:2017-01-17 13:39:20
【问题描述】:

--NodeJS、Mongoose、MongoDB、ExpressJS、EJS--

我的网站是,有一个登录用户,他们可以在其中提交他们想要的“名称”和“图像”,然后作者和其他人可以对该图片发表评论。在每张图像上,我添加了一个函数,我使用 .length 函数计算 cmets,该函数计算图片上的 cmets 并将 cmets 的数量投影到我的 ejs 文件中。

这是我的架构:

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" }

这是我在 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 }

它工作正常,它正在删除评论。这里的问题是,它只在“评论”模型上删除。

我也想在“TestData”模型上删除相同的评论,因为每次我删除评论时,cmets 的计数保持不变。

所以基本上我想删除这两个模型的特定评论。

我尝试使用这种方法:

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");
        }
    });
});

但它不起作用。

你能帮我解决我应该使用什么特定的查询吗?

【问题讨论】:

  • req.params.comment_id 是 cmets 模型的 id 吗?您应该使用不同的 id 来删除 testData。您还可以在您的问题中添加一些示例数据库数据吗?
  • 是的,.comment_id 用于我的“评论”模型上的 id,这是我在 mongodb 上收集的一些示例数据...这是我的 testSchema > db.users.find() { "_id" : ObjectId("57d29ac85643520a946b1ccc"), "name" : "John", "image" : "https://pbs.twimg.com/profile_images/1501070030/John_2011_1_500x500.png", "comments" : [ ObjectId("57d29acf5643520a946b1ccd"), ObjectId("57d2a2d684b1b505188e2807"), ObjectId("57d2a3c43cf15600b436f190"), ObjectId("57d2a42e3cf15600b436f191") ], " author" : { "id" : ObjectId("57d0950d3fd4142528a054e1"), "username" : "johnfrades" }, "__v" : 4 }
  • 这是我的评论Schema > db.comments.find() { "_id" : ObjectId("57d0871c892abb230ca5a728"), "name" : "John F", "text" : "how old are you man", "date" : "9/8/2016 , 9:28 AM", "__v" : 0 } { "_id" : ObjectId("57d092c0d32983243c1c0faf"), "name" : "Mike", "text" : "hey j ohn, what gel did you use?", "date" : "9/8/2016 , 10:20 AM", "__v" : 0 } { "_id" : ObjectId("57d09d94db073f272816ee12"), "text" : "you looked like a crim inal bruh!", "date" : "9/8/2016 , 11:06 AM", "__v" : 0 }谢谢!
  • 请在您的问题中添加。它在 cmets 中不可读。
  • 这两个数据库之间有什么共同的字段吗?或者你在删除时有 testSchema 的 _id 吗?

标签: node.js mongodb express mongoose


【解决方案1】:

试试下面的代码:-

  TestData.update(
             {}, 
             {$pull: {comments: req.params.comment_id}},  
             { multi: true },
             function(err, data){
                  console.log(err, data);
             });

希望这会对你有所帮助。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2012-12-31
    • 2016-08-09
    • 2017-12-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-01-22
    相关资源
    最近更新 更多