Node.js Mongoose删除附加到用户的数组中的特定注释

Node.js Mongoose删除附加到用户的数组中的特定注释,node.js,mongodb,express,mongoose,Node.js,Mongodb,Express,Mongoose,--NodeJS、Mongoose、MongoDB、ExpressJS、EJS-- 我的网站是,有一个登录用户,在那里他们可以提交他们想要的“名字”和“图片”,然后作者和其他人可以对图片发表评论。在每张图片上,我添加了一个函数,我使用.length函数计算图片上的注释,并将注释数投影到我的ejs文件中 这是我的模式: var testSchema = new mongoose.Schema({ name: String, image: String, author: {

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

我的网站是,有一个登录用户,在那里他们可以提交他们想要的“名字”和“图片”,然后作者和其他人可以对图片发表评论。在每张图片上,我添加了一个函数,我使用.length函数计算图片上的注释,并将注释数投影到我的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" }
{ "_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 }
它工作正常,正在删除评论。这里的问题是,它只是在“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");
        }
    });
});
但它不起作用


您能帮助我使用什么特定查询吗?

请尝试以下代码:-

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

希望这能对您有所帮助。

req.params.comment\u id
comments模型的id正确吗?您应该为要删除的testData使用不同的id。您还可以在您的问题中添加一些示例db数据吗?是的,.comment_id用于我的“comment”模型上的id,这是我在mongodb上收集的一些示例数据…这是用于我的testSchema
>db.users.find(){“_id”:ObjectId(“57d29ac85643520a946b1ccc”),“name”:“John”,“image”:"https://pbs.twimg.com/profile_images/1501070030/John_2011_1_500x500.png“,”评论“:[ObjectId(“57d29acf5643520a946b1ccd”)、ObjectId(“57D2A2D684B505188E2807”)、ObjectId(“57d2a3c43cf15600b436f190”)、ObjectId(“57d2a42e3cf15600b436f191”)、”作者“:{“id”:ObjectId(“57d0950d3fd4142528a054e1”)、”用户名“:“johnfrades”}、”和“:4}”
这是我的评论模式
>db.comments.find(){“_id”:ObjectId(“57d0871c892ab230ca5a728”),“name:“John F”,“text:“你多大了”,“date:“9/8/2016,9:28 AM”,“_v”:0}{“_id”:ObjectId(“57d092c0d329c83c243c0faf”),“name:“Mike”,“text:“嘿,你用了什么凝胶?”,“date:”2016年9月8日上午10:20“,”日期“:”2016年9月8日上午11:06“,”日期“,”日期“:”2016年9月8日上午11:06“,”日期“
谢谢!请将其添加到问题中。注释中不可读。这两个数据库之间是否有公共字段?或者删除时是否有testSchema的\u id?