0

我正在使用 nodejs、express、mongo 和 coffeescript,我有一篇带有评论的简单博客文章,我想添加在任何给定时间删除特定评论的可能性。架构如下所示:

Schema = mongoose.Schema

ArticleSchema = new Schema
  title:
    type: String
    trim: true
    required: true

  body:
    type: String
    required: true

  createdAt:
    type: Date
    default: Date.now

  comments: [
    body:
      type: String
      default : ''

    user:
      type: Schema.ObjectId
      ref: 'User'
      required: true

    createdAt:
      type: Date
      default: Date.now
  ]

文章的路线映射如下:

 articles = require '../app/controllers/articles'
 app.get '/', articles.index
 app.get '/articles', articles.manage
 app.get '/articles/new', auth.requiresLogin, articles.new
 app.get '/articles/:articleId', articles.show
 app.get '/articles/:articleId/edit', auth.requiresLogin, articles.edit

 app.param 'articleId', articles.article

但是我该如何做这样的事情才能删除评论呢?

  app.get '/articles/:articleId/comment/:commentId/delete', auth.requiresLogin, articles.edit
4

1 回答 1

0

如果您的意思是应该如何实现评论的删除:您首先使用 检索文章文档articleId,然后您可以找到并删除子文档:

// find article
...

// find the comment by id, and remove it:
article.comments.id(commentId).remove();

// since it's a sub-document of article, you need
// to save the article back to the database to
// 'finalize' the removal of the comment:
article.save(function(err) { ... });
于 2013-07-14T06:27:47.643 回答